boyan-soubachov / tastyworks_api

An unofficial, reverse-engineered Python API for tastyworks.
Apache License 2.0
208 stars 79 forks source link

Runtime error with DataStreamer #40

Closed pratik-r closed 5 years ago

pratik-r commented 5 years ago

Hi,

I ran the following code and got a Runtime error:

from tastyworks.tastyworks_api import tasty_session
from tastyworks.streamer import DataStreamer

client = tasty_session.create_new_session('username', 'password') # no issues here
streamer = DataStreamer(client)

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-9-002b160bbcf0> in <module>
----> 1 streamer = DataStreamer(client)

~\Miniconda3\lib\site-packages\tastyworks\streamer.py in __init__(self, session)
     22         self.subs = {}
     23         asyncio.get_event_loop().run_until_complete(
---> 24             self._setup_connection()
     25         )
     26 

~\Miniconda3\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():

~\Miniconda3\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 running2019-03-17 15:01:46,034 Connecting to url: https://tasty.dxfeed.com/live/cometd
2019-03-17 15:01:46,035 Opening client with connection types ['websocket', 'long-polling'] ...
2019-03-17 15:01:46,222 Connection types supported by the server: ['websocket']
2019-03-17 15:01:46,259 Client opened with connection_type 'websocket'
2019-03-17 15:01:46,297 Subscribed to channel /service/data
2019-03-17 15:01:46,297 Connected and logged in to dxFeed data stream

Any idea what's the cause of this?

Thanks

boyan-soubachov commented 5 years ago

Looking at the error message you get, it seems to me that Miniconda/Jupyter is trying to run your code in an already-existing event loop.

I don't use Jupyter or other Python notebooks so I haven't encountered this issue before. It may be temporarily solved by changing line 23 in streamer.py to just be await self._setup_connection().

Other people seem to be getting this issue as well: https://github.com/jupyter/notebook/issues/3397

pratik-r commented 5 years ago

I just tried making this change, but now I get another error:

Traceback (most recent call last):

  File "<ipython-input-2-ca8a1aab27c7>", line 1, in <module>
    from streamer import DataStreamer

  File "C:\Users\prati\code\streamer.py", line 23
    await self._setup_connection()
    ^
SyntaxError: 'await' outside async function

I removed the await and the code works now, but I get a runtime warning:

streamer.py:23: RuntimeWarning: coroutine 'DataStreamer._setup_connection' was never awaited
  self._setup_connection()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
pratik-r commented 5 years ago

For other IPython users facing this problem, the following worked for me: Install nest_asyncio, then run:

import nest_asyncio
nest_asyncio.apply()