The cryptofeed is a python library that uses asyncio library to get real-time prices of different Crypto Exchanges. In this short program, we try to run the cryptofeed FeedHandler in an independent thread. An example of the code is shown below:
import functools as fct
from cryptofeed import FeedHandler
from cryptofeed.defines import BID, ASK, L2_BOOK
from cryptofeed.exchanges import Kraken
from datetime import datetime
import threading
async def bookfunc(params , orderbooks, feed, symbol, book, timestamp, receipt_timestamp):
print(f'Timestamp: {timestamp} Cryptofeed Receipt: {receipt_timestamp} Feed: {feed} Symbol: {symbol}'
f' Book Bid Size is {len(book[BID])} Ask Size is {len(book[ASK])}')
orderbooks = filter_orderbook(orderbooks, book, symbol, params['orderbook']['depth'])
def func():
# Parameters
params = {'orderbook': {'depth': 2}, 'price_model':{}, 'trade_model': {}}
config = {'log': {'filename': 'logs/demo.log', 'level': 'INFO'}}
orderbooks = {}
f = FeedHandler(config=config)
f.add_feed(Kraken(checksum_validation=True, subscription={L2_BOOK: ['BTC-USD', 'ETH-USD', 'LINK-USD', 'LTC-USD', 'ADA-USD']},
callbacks={L2_BOOK: fct.partial(bookfunc, params, orderbooks)})) # This way passes the orderbooks inside the callback
f.run()
if __name__ == '__main__':
thread = threading.Thread(target=func, args=())
thread.start()
When the code is executed, the get the following error:
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-1'.
Any idea how to solve this problem?
EDIT: This is the solution for different questions in stackoverflow. An example is the following Question: