Python asyncio.BaseEventLoop() Examples
The following are 30
code examples of asyncio.BaseEventLoop().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
asyncio
, or try the search function
.
Example #1
Source File: cascade.py From batch-shipyard with MIT License | 6 votes |
def _record_perf_async( loop: asyncio.BaseEventLoop, event: str, message: str) -> None: """Record timing metric async :param asyncio.BaseEventLoop loop: event loop :param str event: event :param str message: message """ if not _RECORD_PERF: return proc = await asyncio.create_subprocess_shell( './perf.py cascade {ev} --prefix {pr} --message "{msg}"'.format( ev=event, pr=_PREFIX, msg=message), loop=loop) await proc.wait() if proc.returncode != 0: logger.error( 'could not record perf to storage for event: {}'.format(event))
Example #2
Source File: dolomite_order_book_tracker.py From hummingbot with Apache License 2.0 | 6 votes |
def __init__( self, data_source_type: OrderBookTrackerDataSourceType = OrderBookTrackerDataSourceType.EXCHANGE_API, trading_pairs: Optional[List[str]] = None, rest_api_url: str = "", websocket_url: str = "", ): super().__init__(data_source_type=data_source_type) self._order_books: Dict[str, DolomiteOrderBook] = {} self._saved_message_queues: Dict[str, Deque[DolomiteOrderBookMessage]] = defaultdict(lambda: deque(maxlen=1000)) self._order_book_snapshot_stream: asyncio.Queue = asyncio.Queue() self._ev_loop: asyncio.BaseEventLoop = asyncio.get_event_loop() self._data_source: Optional[OrderBookTrackerDataSource] = None self._active_order_trackers: Dict[str, DolomiteActiveOrderTracker] = defaultdict(DolomiteActiveOrderTracker) self._trading_pairs: Optional[List[str]] = trading_pairs self.rest_api_url = rest_api_url self.websocket_url = websocket_url
Example #3
Source File: run.py From paco with MIT License | 6 votes |
def run(coro, loop=None): """ Convenient shortcut alias to ``loop.run_until_complete``. Arguments: coro (coroutine): coroutine object to schedule. loop (asyncio.BaseEventLoop): optional event loop to use. Defaults to: ``asyncio.get_event_loop()``. Returns: mixed: returned value by coroutine. Usage:: async def mul_2(num): return num * 2 paco.run(mul_2(4)) # => 8 """ loop = loop or asyncio.get_event_loop() return loop.run_until_complete(coro)
Example #4
Source File: cascade.py From cortana-intelligence-inventory-optimization with MIT License | 6 votes |
def _renew_queue_message_lease( loop: asyncio.BaseEventLoop, queue_client: azure.storage.queue.QueueService, queue_key: str, cb_key: str, msg_id: str): """Renew a storage queue message lease :param asyncio.BaseEventLoop loop: event loop :param azure.storage.queue.QueueService queue_client: queue client :param str queue_key: queue name key index into _STORAGE_CONTAINERS :param str cb_key: callback handle key :param str msg_id: message id """ msg = queue_client.update_message( _STORAGE_CONTAINERS[queue_key], message_id=msg_id, pop_receipt=_QUEUE_MESSAGES[msg_id], visibility_timeout=45) if msg.pop_receipt is None: raise RuntimeError( 'update message failed for id={} pr={}'.format( msg_id, _QUEUE_MESSAGES[msg_id])) _QUEUE_MESSAGES[msg_id] = msg.pop_receipt _CBHANDLES[cb_key] = loop.call_later( 15, _renew_queue_message_lease, loop, queue_client, queue_key, cb_key, msg_id)
Example #5
Source File: kraken_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 6 votes |
def listen_for_trades(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): while True: try: ws_message: str = await self.get_ws_subscription_message("trade") async with websockets.connect(DIFF_STREAM_URL) as ws: ws: websockets.WebSocketClientProtocol = ws await ws.send(ws_message) async for raw_msg in self._inner_messages(ws): msg: List[Any] = ujson.loads(raw_msg) trades: List[Dict[str, Any]] = [{"pair": msg[-1], "trade": trade} for trade in msg[1]] for trade in trades: trade_msg: OrderBookMessage = KrakenOrderBook.trade_message_from_exchange(trade) output.put_nowait(trade_msg) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error with WebSocket connection. Retrying after 30 seconds...", exc_info=True) await asyncio.sleep(30.0)
Example #6
Source File: bitfinex_api_user_stream_data_source.py From hummingbot with Apache License 2.0 | 6 votes |
def listen_for_user_stream(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): while True: try: ws: websockets.WebSocketClientProtocol async with websockets.connect(BITFINEX_WS_URI) as ws: ws: websockets.WebSocketClientProtocol = ws payload = self._bitfinex_auth.generate_auth_payload() await ws.send(json.dumps(payload)) async for raw_msg in self._get_response(ws): transformed_msg: BitfinexOrderBookMessage = self._transform_message_from_exchange(raw_msg) if transformed_msg: output.put_nowait(transformed_msg) except asyncio.CancelledError: raise except Exception: self.logger().error( "Unexpected error with Bitfinex WebSocket connection. " "Retrying after 30 seconds...", exc_info=True, ) await asyncio.sleep(self.MESSAGE_TIMEOUT)
Example #7
Source File: cascade.py From cortana-intelligence-inventory-optimization with MIT License | 6 votes |
def _record_perf_async( loop: asyncio.BaseEventLoop, event: str, message: str) -> None: """Record timing metric async :param asyncio.BaseEventLoop loop: event loop :param str event: event :param str message: message """ if not _RECORD_PERF: return proc = await asyncio.subprocess.create_subprocess_shell( './perf.py cascade {ev} --prefix {pr} --message "{msg}"'.format( ev=event, pr=_PREFIX, msg=message), loop=loop) await proc.wait() if proc.returncode != 0: logger.error( 'could not record perf to storage for event: {}'.format(event))
Example #8
Source File: binance_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 6 votes |
def listen_for_order_book_diffs(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): while True: try: trading_pairs: List[str] = await self.get_trading_pairs() ws_path: str = "/".join([f"{trading_pair.lower()}@depth" for trading_pair in trading_pairs]) stream_url: str = f"{DIFF_STREAM_URL}/{ws_path}" async with websockets.connect(stream_url) as ws: ws: websockets.WebSocketClientProtocol = ws async for raw_msg in self._inner_messages(ws): msg = ujson.loads(raw_msg) order_book_message: OrderBookMessage = BinanceOrderBook.diff_message_from_exchange( msg, time.time()) output.put_nowait(order_book_message) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error with WebSocket connection. Retrying after 30 seconds...", exc_info=True) await asyncio.sleep(30.0)
Example #9
Source File: pipeline.py From batchflow with Apache License 2.0 | 6 votes |
def execute_for(self, batch, new_loop=False): """ Run a pipeline for one batch Parameters ---------- batch an input batch new_loop : bool whether to create a new :class:`async loop <asyncio.BaseEventLoop>`. Returns ------- a batch - an output from the last action in the pipeline """ if new_loop: asyncio.set_event_loop(asyncio.new_event_loop()) batch.pipeline = self batch_res = self._exec_all_actions(batch) batch_res.pipeline = self return batch_res
Example #10
Source File: kucoin_order_book_tracker.py From hummingbot with Apache License 2.0 | 5 votes |
def __init__(self, data_source_type: OrderBookTrackerDataSourceType = OrderBookTrackerDataSourceType.EXCHANGE_API, trading_pairs: Optional[List[str]] = None): super().__init__(data_source_type=data_source_type) self._order_book_diff_stream: asyncio.Queue = asyncio.Queue() self._order_book_snapshot_stream: asyncio.Queue = asyncio.Queue() self._ev_loop: asyncio.BaseEventLoop = asyncio.get_event_loop() self._data_source: Optional[OrderBookTrackerDataSource] = None self._saved_message_queues: Dict[str, Deque[KucoinOrderBookMessage]] = defaultdict(lambda: deque(maxlen=1000)) self._active_order_trackers: Dict[str, KucoinActiveOrderTracker] = defaultdict(KucoinActiveOrderTracker) self._trading_pairs: Optional[List[str]] = trading_pairs
Example #11
Source File: kucoin_api_user_stream_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def listen_for_user_stream(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): try: while True: try: if self._current_listen_key is None: creds = await self.get_listen_key() self._current_listen_key = creds["data"]["token"] self._current_endpoint = creds["data"]["instanceServers"][0]["endpoint"] self.logger().debug(f"Obtained listen key {self._current_listen_key}.") if self._listen_for_user_stream_task is not None: self._listen_for_user_stream_task.cancel() self._listen_for_user_stream_task = safe_ensure_future(self.log_user_stream(output)) await self.wait_til_next_tick(seconds=40.0) success: bool = False async with (await self.get_ws_connection()) as ws2: success = await self.ping_listen_key(ws2) if not success: print("No pong") self._current_listen_key = None if self._listen_for_user_stream_task is not None: self._listen_for_user_stream_task.cancel() self._listen_for_user_stream_task = None continue self.logger().debug(f"Refreshed listen key {self._current_listen_key}.") await self.wait_til_next_tick(seconds=40.0) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error while maintaining the user event listen key. Retrying after " "5 seconds...", exc_info=True) await asyncio.sleep(5) finally: # Make sure no background task is leaked. if self._listen_for_user_stream_task is not None: self._listen_for_user_stream_task.cancel() self._listen_for_user_stream_task = None
Example #12
Source File: bamboo_relay_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def listen_for_trades(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): # Trade messages are received from the order book web socket pass
Example #13
Source File: bamboo_relay_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def listen_for_order_book_snapshots(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): while True: try: trading_pairs: List[str] = await self.get_trading_pairs() client: aiohttp.ClientSession = self.http_client() for trading_pair in trading_pairs: try: snapshot: Dict[str, any] = await self.get_snapshot(client, trading_pair, self._api_endpoint, self._api_prefix) snapshot_timestamp: float = time.time() snapshot_msg: OrderBookMessage = BambooRelayOrderBook.snapshot_message_from_exchange( snapshot, snapshot_timestamp, metadata={"trading_pair": trading_pair} ) output.put_nowait(snapshot_msg) self.logger().debug(f"Saved order book snapshot for {trading_pair}") await asyncio.sleep(5.0) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error.", exc_info=True) await asyncio.sleep(5.0) this_hour: pd.Timestamp = pd.Timestamp.utcnow().replace(minute=0, second=0, microsecond=0) next_hour: pd.Timestamp = this_hour + pd.Timedelta(hours=1) delta: float = next_hour.timestamp() - time.time() await asyncio.sleep(delta) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error.", exc_info=True) await asyncio.sleep(5.0)
Example #14
Source File: binance_api_user_stream_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def listen_for_user_stream(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): try: while True: try: if self._current_listen_key is None: self._current_listen_key = await self.get_listen_key() self.logger().debug(f"Obtained listen key {self._current_listen_key}.") if self._listen_for_user_stream_task is not None: self._listen_for_user_stream_task.cancel() self._listen_for_user_stream_task = safe_ensure_future(self.log_user_stream(output)) await self.wait_til_next_tick(seconds=60.0) success: bool = await self.ping_listen_key(self._current_listen_key) if not success: self._current_listen_key = None if self._listen_for_user_stream_task is not None: self._listen_for_user_stream_task.cancel() self._listen_for_user_stream_task = None continue self.logger().debug(f"Refreshed listen key {self._current_listen_key}.") await self.wait_til_next_tick(seconds=60.0) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error while maintaining the user event listen key. Retrying after " "5 seconds...", exc_info=True) await asyncio.sleep(5) finally: # Make sure no background task is leaked. if self._listen_for_user_stream_task is not None: self._listen_for_user_stream_task.cancel() self._listen_for_user_stream_task = None self._current_listen_key = None
Example #15
Source File: huobi_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def listen_for_order_book_snapshots(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): while True: try: trading_pairs: List[str] = await self.get_trading_pairs() async with aiohttp.ClientSession() as client: for trading_pair in trading_pairs: try: snapshot: Dict[str, Any] = await self.get_snapshot(client, trading_pair) snapshot_message: OrderBookMessage = HuobiOrderBook.snapshot_message_from_exchange( snapshot, metadata={"trading_pair": trading_pair} ) output.put_nowait(snapshot_message) self.logger().debug(f"Saved order book snapshot for {trading_pair}") await asyncio.sleep(5.0) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error.", exc_info=True) await asyncio.sleep(5.0) this_hour: pd.Timestamp = pd.Timestamp.utcnow().replace(minute=0, second=0, microsecond=0) next_hour: pd.Timestamp = this_hour + pd.Timedelta(hours=1) delta: float = next_hour.timestamp() - time.time() await asyncio.sleep(delta) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error.", exc_info=True) await asyncio.sleep(5.0)
Example #16
Source File: bitfinex_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def listen_for_trades(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): trading_pairs: List[str] = await self.get_trading_pairs() tasks = [ ev_loop.create_task(self._listen_trades_for_pair(pair, output)) for pair in trading_pairs ] await asyncio.gather(*tasks)
Example #17
Source File: bitfinex_order_book_tracker.py From hummingbot with Apache License 2.0 | 5 votes |
def __init__(self, data_source_type: OrderBookTrackerDataSourceType = EXC_API, trading_pairs: Optional[List[str]] = None): super().__init__(data_source_type=data_source_type) self._order_book_diff_stream: asyncio.Queue = asyncio.Queue() self._order_book_snapshot_stream: asyncio.Queue = asyncio.Queue() self._ev_loop: asyncio.BaseEventLoop = asyncio.get_event_loop() self._data_source: Optional[OrderBookTrackerDataSource] = None self._saved_message_queues: QUEUE_TYPE = defaultdict( lambda: deque(maxlen=SAVED_MESSAGES_QUEUE_SIZE) ) self._trading_pairs: Optional[List[str]] = trading_pairs self._active_order_trackers: TRACKER_TYPE = defaultdict(BitfinexActiveOrderTracker)
Example #18
Source File: bitfinex_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def listen_for_order_book_diffs(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): trading_pairs: List[str] = await self.get_trading_pairs() tasks = [ self._listen_order_book_for_pair(pair, output) for pair in trading_pairs ] await asyncio.gather(*tasks)
Example #19
Source File: huobi_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def listen_for_trades(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): while True: try: trading_pairs: List[str] = await self.get_trading_pairs() async with websockets.connect(HUOBI_WS_URI) as ws: ws: websockets.WebSocketClientProtocol = ws for trading_pair in trading_pairs: subscribe_request: Dict[str, Any] = { "sub": f"market.{trading_pair}.trade.detail", "id": trading_pair } await ws.send(json.dumps(subscribe_request)) async for raw_msg in self._inner_messages(ws): # Huobi compresses their ws data encoded_msg: bytes = gzip.decompress(raw_msg) # Huobi's data value for id is a large int too big for ujson to parse msg: Dict[str, Any] = json.loads(encoded_msg.decode('utf-8')) if "ping" in msg: await ws.send(f'{{"op":"pong","ts": {str(msg["ping"])}}}') elif "subbed" in msg: pass elif "ch" in msg: trading_pair = msg["ch"].split(".")[1] for data in msg["tick"]["data"]: trade_message: OrderBookMessage = HuobiOrderBook.trade_message_from_exchange( data, metadata={"trading_pair": trading_pair} ) output.put_nowait(trade_message) else: self.logger().debug(f"Unrecognized message received from Huobi websocket: {msg}") except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error with WebSocket connection. Retrying after 30 seconds...", exc_info=True) await asyncio.sleep(30.0)
Example #20
Source File: kucoin_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def listen_for_order_book_snapshots(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): while True: try: trading_pairs: List[str] = await self.get_trading_pairs() async with aiohttp.ClientSession() as client: for trading_pair in trading_pairs: try: snapshot: Dict[str, Any] = await self.get_snapshot(client, trading_pair) snapshot_timestamp: float = time.time() snapshot_msg: OrderBookMessage = KucoinOrderBook.snapshot_message_from_exchange( snapshot, snapshot_timestamp, metadata={"symbol": trading_pair} ) output.put_nowait(snapshot_msg) self.logger().debug(f"Saved order book snapshot for {trading_pair}") await asyncio.sleep(5.0) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error.", exc_info=True) await asyncio.sleep(5.0) this_hour: pd.Timestamp = pd.Timestamp.utcnow().replace(minute=0, second=0, microsecond=0) next_hour: pd.Timestamp = this_hour + pd.Timedelta(hours=1) delta: float = next_hour.timestamp() - time.time() await asyncio.sleep(delta) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error.", exc_info=True) await asyncio.sleep(5.0)
Example #21
Source File: kucoin_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def listen_for_order_book_diffs(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): websocket_data: Dict[str, Any] = await self.ws_connect_data() kucoin_ws_uri: str = websocket_data["data"]["instanceServers"][0]["endpoint"] + "?token=" + websocket_data["data"]["token"] + "&acceptUserMessage=true" while True: try: trading_pairs: List[str] = await self.get_trading_pairs() async with websockets.connect(kucoin_ws_uri) as ws: ws: websockets.WebSocketClientProtocol = ws for trading_pair in trading_pairs: subscribe_request: Dict[str, Any] = { "id": int(time.time()), "type": "subscribe", "topic": f"/market/level2:{trading_pair}", "response": True } await ws.send(json.dumps(subscribe_request)) async for raw_msg in self._inner_messages(ws): msg: Dict[str, Any] = json.loads(raw_msg) if msg["type"] == "pong" or msg["type"] == "ack": pass elif msg["type"] == "message": order_book_message: OrderBookMessage = KucoinOrderBook.diff_message_from_exchange(msg) output.put_nowait(order_book_message) else: self.logger().debug(f"Unrecognized message received from Kucoin websocket: {msg}") except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error with WebSocket connection. Retrying after 30 seconds...", exc_info=True) await asyncio.sleep(30.0)
Example #22
Source File: kucoin_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def listen_for_trades(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): websocket_data: Dict[str, Any] = await self.ws_connect_data() kucoin_ws_uri: str = websocket_data["data"]["instanceServers"][0]["endpoint"] + "?token=" + websocket_data["data"]["token"] + "&acceptUserMessage=true" while True: try: trading_pairs: List[str] = await self.get_trading_pairs() async with websockets.connect(kucoin_ws_uri) as ws: ws: websockets.WebSocketClientProtocol = ws for trading_pair in trading_pairs: subscribe_request: Dict[str, Any] = { "id": int(time.time()), "type": "subscribe", "topic": f"/market/match:{trading_pair}", "privateChannel": False, "response": True } await ws.send(json.dumps(subscribe_request)) async for raw_msg in self._inner_messages(ws): msg: Dict[str, Any] = json.loads(raw_msg) if msg["type"] == "pong" or msg["type"] == "ack": pass elif msg["type"] == "message": trading_pair = msg["data"]["symbol"] data = msg["data"] trade_message: OrderBookMessage = KucoinOrderBook.trade_message_from_exchange( data, metadata={"trading_pair": trading_pair} ) output.put_nowait(trade_message) else: self.logger().debug(f"Unrecognized message received from Kucoin websocket: {msg}") except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error with WebSocket connection. Retrying after 30 seconds...", exc_info=True) await asyncio.sleep(30.0)
Example #23
Source File: mock_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def listen_for_trades(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): pass
Example #24
Source File: kraken_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def listen_for_order_book_snapshots(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): while True: try: trading_pairs: List[str] = await self.get_trading_pairs() async with aiohttp.ClientSession() as client: for trading_pair in trading_pairs: try: snapshot: Dict[str, Any] = await self.get_snapshot(client, trading_pair) snapshot_timestamp: float = time.time() snapshot_msg: OrderBookMessage = KrakenOrderBook.snapshot_message_from_exchange( snapshot, snapshot_timestamp, metadata={"trading_pair": trading_pair} ) output.put_nowait(snapshot_msg) self.logger().debug(f"Saved order book snapshot for {trading_pair}") await asyncio.sleep(5.0) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error. ", exc_info=True) await asyncio.sleep(5.0) this_hour: pd.Timestamp = pd.Timestamp.utcnow().replace(minute=0, second=0, microsecond=0) next_hour: pd.Timestamp = this_hour + pd.Timedelta(hours=1) delta: float = next_hour.timestamp() - time.time() await asyncio.sleep(delta) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error. ", exc_info=True) await asyncio.sleep(5.0)
Example #25
Source File: kraken_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def listen_for_order_book_diffs(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): while True: try: ws_message: str = await self.get_ws_subscription_message("book") async with websockets.connect(DIFF_STREAM_URL) as ws: ws: websockets.WebSocketClientProtocol = ws await ws.send(ws_message) async for raw_msg in self._inner_messages(ws): msg = ujson.loads(raw_msg) msg_dict = {"trading_pair": msg[-1], "asks": msg[1].get("a", []) or msg[1].get("as", []) or [], "bids": msg[1].get("b", []) or msg[1].get("bs", []) or []} msg_dict["update_id"] = max([*map(lambda x: float(x[2]), msg_dict["bids"] + msg_dict["asks"])], default=0.) if "as" in msg[1] and "bs" in msg[1]: order_book_message: OrderBookMessage = KrakenOrderBook.snapshot_ws_message_from_exchange( msg_dict, time.time()) else: order_book_message: OrderBookMessage = KrakenOrderBook.diff_message_from_exchange( msg_dict, time.time()) output.put_nowait(order_book_message) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error with WebSocket connection. Retrying after 30 seconds...", exc_info=True) await asyncio.sleep(30.0)
Example #26
Source File: kraken_api_user_stream_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def listen_for_user_stream(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): while True: try: async with websockets.connect(KRAKEN_WS_URL) as ws: ws: websockets.WebSocketClientProtocol = ws if self._current_auth_token is None: self._current_auth_token = await self.get_auth_token() for subscription_type in ["openOrders", "ownTrades"]: subscribe_request: Dict[str, Any] = { "event": "subscribe", "subscription": { "name": subscription_type, "token": self._current_auth_token } } await ws.send(ujson.dumps(subscribe_request)) async for raw_msg in self._inner_messages(ws): self._last_recv_time = time.time() diff_msg = ujson.loads(raw_msg) output.put_nowait(diff_msg) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error with Kraken WebSocket connection. " "Retrying after 30 seconds...", exc_info=True) self._current_auth_token = None await asyncio.sleep(30.0)
Example #27
Source File: hummingbot_application.py From hummingbot with Apache License 2.0 | 5 votes |
def __init__(self): self.ev_loop: asyncio.BaseEventLoop = asyncio.get_event_loop() self.parser: ThrowingArgumentParser = load_parser(self) self.app = HummingbotCLI( input_handler=self._handle_command, bindings=load_key_bindings(self), completer=load_completer(self) ) self.markets: Dict[str, MarketBase] = {} self.wallet: Optional[Web3Wallet] = None # strategy file name and name get assigned value after import or create command self.strategy_file_name: str = None self.strategy_name: str = None self.strategy_task: Optional[asyncio.Task] = None self.strategy: Optional[StrategyBase] = None self.market_pair: Optional[CrossExchangeMarketPair] = None self.market_trading_pair_tuples: List[MarketTradingPairTuple] = [] self.clock: Optional[Clock] = None self.init_time: int = int(time.time() * 1e3) self.start_time: Optional[int] = None self.assets: Optional[Set[str]] = set() self.starting_balances = {} self.placeholder_mode = False self.log_queue_listener: Optional[logging.handlers.QueueListener] = None self.data_feed: Optional[DataFeedBase] = None self.notifiers: List[NotifierBase] = [] self.kill_switch: Optional[KillSwitch] = None self._app_warnings: Deque[ApplicationWarning] = deque() self._trading_required: bool = True self.trade_fill_db: SQLConnectionManager = SQLConnectionManager.get_trade_fills_instance() self.markets_recorder: Optional[MarketsRecorder] = None
Example #28
Source File: db.py From code-jam-5 with MIT License | 5 votes |
def __init__(self, loop: asyncio.BaseEventLoop = None): self.connection: sqlite3.Connection = None self.loop = loop or asyncio.get_event_loop()
Example #29
Source File: test_utils.py From lambda-text-extractor with Apache License 2.0 | 5 votes |
def setup_test_loop(loop_factory=asyncio.new_event_loop): """Create and return an asyncio.BaseEventLoop instance. The caller should also call teardown_test_loop, once they are done with the loop. """ loop = loop_factory() asyncio.set_event_loop(None) return loop
Example #30
Source File: test_utils.py From lambda-text-extractor with Apache License 2.0 | 5 votes |
def setup_test_loop(loop_factory=asyncio.new_event_loop): """Create and return an asyncio.BaseEventLoop instance. The caller should also call teardown_test_loop, once they are done with the loop. """ loop = loop_factory() asyncio.set_event_loop(None) return loop