cryptofeed 是一个 Python 库,它使用 asyncio 库来获取不同加密货币交易所的实时价格。 在这个简短的程序中,我们尝试在一个独立线程中运行 cryptofeed FeedHandler。代码示例如下所示:
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,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()
代码执行时,得到如下错误:
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-1'.
知道如何解决这个问题吗?
编辑: 这是stackoverflow中不同问题的解决方案。一个例子是以下问题: