alpaca-trade-api-python icon indicating copy to clipboard operation
alpaca-trade-api-python copied to clipboard

`'NoneType' object has no attribute 'consume'` error when call conn.close()

Open xiaoconghua opened this issue 5 years ago • 1 comments

Here is the simple example I have.

    conn = StreamConn()
    print("Create a thread for the client")
    conn_thread = threading.Thread(target=conn.run)
    print("Before thread start")
    conn_thread.start()
    print("After thread start")

    print("Sleep for 1second")
    time.sleep(1)

    loop = conn.loop
    loop.run_until_complete(conn.close(renew=False))

The error message is

Create a thread for the client
Before thread start
After thread start
Sleep for 1second
Traceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py", line 192, in _run_module_as_main
2021-02-03 21:42:45,786 root [ERROR] error while consuming ws messages: 'NoneType' object has no attribute 'consume'
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/USER/pyalgotrade/user_strategies/test/test_stream_client.py", line 168, in <module>
    loop.run_until_complete(client.close(renew=False))
  File "/usr/lib/python3.8/asyncio/base_events.py", line 595, in run_until_complete
    self.run_forever()
  File "/usr/lib/python3.8/asyncio/base_events.py", line 550, in run_forever
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running

This is the simplest case I can get. Some help or guidance would be really appreciated.

xiaoconghua avatar Feb 04 '21 05:02 xiaoconghua

Hi, I added best practices for the streamconn object, plus enabling the ability to stop the connection in this PR: https://github.com/alpacahq/alpaca-trade-api-python/pull/379

It will probably take some time until it will be released to pypi. you could install it directly from github for now for your usage

shlomiku avatar Feb 09 '21 12:02 shlomiku