GithubHelp home page GithubHelp logo

Comments (2)

JacobPlaster avatar JacobPlaster commented on July 18, 2024

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()

from bitfinex-api-py.

DmitriyG228 avatar DmitriyG228 commented on July 18, 2024

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`

from bitfinex-api-py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.