Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I close a client.ws conection? #63

Closed
marwernicke opened this issue Apr 7, 2020 · 2 comments
Closed

How do I close a client.ws conection? #63

marwernicke opened this issue Apr 7, 2020 · 2 comments

Comments

@marwernicke
Copy link

I don't know how to close my websocket connection, so in case of an exception the program shuts down for being able to run again.

Here is a simplified version of code

I just want to open the connection, wait a few seconds and then close it

bfx = Client(API_KEY, API_SECRET)

@bfx.ws.on('all')
async def log_updates(notification):
      print(notification)

bfx.ws.run()
time.sleep(5)
bfx.ws.stop()

Can somebody tell me how to do this? I'm new to programming and have tried lots of stuff unsuccessfully

@JacobPlaster
Copy link
Contributor

JacobPlaster commented Apr 16, 2020

A close websocket function was added in this PR #63.

Please see this example on how to use:

import os
import sys
sys.path.append('../../../')

from bfxapi import Client

bfx = Client(
  logLevel='DEBUG',
)

@bfx.ws.on('order_book_snapshot')
async def log_snapshot(data):
  print ("Snapshot: {}".format(data))
  # stop the websocket once a snapshot is received
  await bfx.ws.stop()

async def start():
  await bfx.ws.subscribe('book', 'tBTCUSD')

bfx.ws.on('connected', start)
bfx.ws.run()

@DmitriyG228
Copy link

DmitriyG228 commented Mar 19, 2021

Hi, this function works on specific event like 'order_book_snapshot', but how to close connection from the main thread on demand?

await bfx.ws.stop()

`---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
in
----> 1 await bfx.ws.stop()

~/anaconda3/envs/arb/lib/python3.8/site-packages/bfxapi/websockets/generic_websocket.py in stop(self)
164 self.attempt_retry = False
165 for key, socket in self.sockets.items():
--> 166 await socket.ws.close()
167 self._emit('done')
168

~/anaconda3/envs/arb/lib/python3.8/site-packages/websockets/protocol.py in close(self, code, reason)
530 """
531 try:
--> 532 yield from asyncio.wait_for(
533 self.write_close_frame(serialize_close(code, reason)),
534 self.close_timeout,

~/anaconda3/envs/arb/lib/python3.8/asyncio/tasks.py in wait_for(fut, timeout, loop)
474 # wait until the future completes or the timeout
475 try:
--> 476 await waiter
477 except exceptions.CancelledError:
478 fut.remove_done_callback(cb)

RuntimeError: Task <Task pending name='Task-5' coro=<InteractiveShell.run_cell_async() running at /home/dima/anaconda3/envs/arb/lib/python3.8/site-packages/IPython/core/interactiveshell.py:3155> cb=[IPythonKernel._cancel_on_sigint..cancel_unless_done(<Future pendi...ernel.py:230]>)() at /home/dima/anaconda3/envs/arb/lib/python3.8/site-packages/ipykernel/ipkernel.py:230, IOLoop.add_future..() at /home/dima/anaconda3/envs/arb/lib/python3.8/site-packages/tornado/ioloop.py:688]> got Future attached to a different loop`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants