def run(self) -> None: """ New process created to instantiate limit order books for (1) Coinbase Pro, and (2) Bitfinex. Connections made to each exchange are made asynchronously thanks to asyncio. :return: void """ coinbase, bitfinex = self.symbols self.workers[coinbase] = CoinbaseClient(sym=coinbase) self.workers[bitfinex] = BitfinexClient(sym=bitfinex) self.workers[coinbase].start(), self.workers[bitfinex].start() Timer(5.0, self.timer_worker, args=( self.workers[coinbase], self.workers[bitfinex], )).start() tasks = asyncio.gather( *[self.workers[sym].subscribe() for sym in self.workers.keys()]) loop = asyncio.get_event_loop() LOGGER.info('Recorder: Gathered %i tasks' % len(self.workers.keys())) try: loop.run_until_complete(tasks) loop.close() [self.workers[sym].join() for sym in self.workers.keys()] LOGGER.info('Recorder: loop closed for %s and %s.' % (coinbase, bitfinex)) except KeyboardInterrupt as e: LOGGER.info("Recorder: Caught keyboard interrupt. \n%s" % e) tasks.cancel() loop.close() [self.workers[sym].join() for sym in self.workers.keys()] finally: loop.close() LOGGER.info('Recorder: Finally done for %s and %s.' % (coinbase, bitfinex))
import asyncio from data_recorder.bitfinex_connector.bitfinex_client import BitfinexClient if __name__ == "__main__": """ This __main__ function is used for testing the BitfinexClient class in isolation. """ symbols = ['tBTCUSD'] # 'tETHUSD', 'tLTCUSD'] print('Initializing...%s' % symbols) loop = asyncio.get_event_loop() p = dict() for sym in symbols: p[sym] = BitfinexClient(sym) p[sym].start() print('Started thread for %s' % sym) tasks = asyncio.gather(*[(p[sym].subscribe()) for sym in symbols]) print('Gathered %i tasks' % len(symbols)) try: loop.run_until_complete(tasks) for sym in symbols: p[sym].join() print('Closing [%s]' % p[sym].name) print('loop closed.') except KeyboardInterrupt as e: print("Caught keyboard interrupt. Canceling tasks...")