mariostoev / finviz

Unofficial API for finviz.com
MIT License
1.06k stars 277 forks source link

Faced "RuntimeError: This event loop is already running" in Jupyter notebook #82

Closed wyfok closed 2 years ago

wyfok commented 3 years ago

When I run

stock_list = Screener(tickers=['AAPL'], table='Performance', order='price')

in Jupyter notebook, it prompts up "Runtime Error" message. However, this problem seems only to happen in Jupyter notebook.

Error message as below

RuntimeError Traceback (most recent call last)

in ----> 1 stock_list = Screener(tickers=['AAPL'], table='Performance', order='price') ~\Anaconda3\lib\site-packages\finviz\screener.py in __init__(self, tickers, filters, rows, order, signal, table, custom) 118 119 self.analysis = [] --> 120 self.data = self.__search_screener() 121 122 def __call__(self, tickers=None, filters=None, rows=None, order='', signal='', table=None, custom=None): ~\Anaconda3\lib\site-packages\finviz\screener.py in __search_screener(self) 323 self.headers, 324 self._rows) --> 325 pages_data = async_connector.run_connector() 326 327 data = [] ~\Anaconda3\lib\site-packages\finviz\helper_functions\request_functions.py in run_connector(self) 77 78 loop = asyncio.get_event_loop() ---> 79 loop.run_until_complete(self.__async_scraper()) 80 81 return self.data ~\Anaconda3\lib\asyncio\base_events.py in run_until_complete(self, future) 569 future.add_done_callback(_run_until_complete_cb) 570 try: --> 571 self.run_forever() 572 except: 573 if new_task and future.done() and not future.cancelled(): ~\Anaconda3\lib\asyncio\base_events.py in run_forever(self) 524 self._check_closed() 525 if self.is_running(): --> 526 raise RuntimeError('This event loop is already running') 527 if events._get_running_loop() is not None: 528 raise RuntimeError( RuntimeError: This event loop is already running
benstear commented 3 years ago

I am also getting this error when running the example code in the README in a Jupiter notebook.

RuntimeError Traceback (most recent call last)

in 1 filters = ['exch_nasd', 'idx_sp500'] # Shows companies in NASDAQ which are in the S&P500 ----> 2 stock_list = Screener(filters=filters, table='Performance', order='price') # Get the performance table and sort it by price ascending 3 4 5 for stock in stock_list[9:19]: # Loop through 10th - 20th stocks ~/opt/anaconda3/lib/python3.7/site-packages/finviz/screener.py in __init__(self, tickers, filters, rows, order, signal, table, custom) 118 119 self.analysis = [] --> 120 self.data = self.__search_screener() 121 122 def __call__(self, tickers=None, filters=None, rows=None, order='', signal='', table=None, custom=None): ~/opt/anaconda3/lib/python3.7/site-packages/finviz/screener.py in __search_screener(self) 323 self.headers, 324 self._rows) --> 325 pages_data = async_connector.run_connector() 326 327 data = [] ~/opt/anaconda3/lib/python3.7/site-packages/finviz/helper_functions/request_functions.py in run_connector(self) 77 78 loop = asyncio.get_event_loop() ---> 79 loop.run_until_complete(self.__async_scraper()) 80 81 return self.data ~/opt/anaconda3/lib/python3.7/asyncio/base_events.py in run_until_complete(self, future) 564 future.add_done_callback(_run_until_complete_cb) 565 try: --> 566 self.run_forever() 567 except: 568 if new_task and future.done() and not future.cancelled(): ~/opt/anaconda3/lib/python3.7/asyncio/base_events.py in run_forever(self) 519 self._check_closed() 520 if self.is_running(): --> 521 raise RuntimeError('This event loop is already running') 522 if events._get_running_loop() is not None: 523 raise RuntimeError( RuntimeError: This event loop is already running
ariahrism commented 3 years ago

I reported this same symptom, via #58

Are you using Google colab by chance?

johnjoo1 commented 3 years ago

A workaround is adding the following before you run:

import nest_asyncio
nest_asyncio.apply()
benstear commented 3 years ago

This fixed the problem for me.