协程从未等待切换到新计算机后

时间:2019-06-13 06:19:47

标签: python asynchronous ib-api

代码在ib insync 0.9.34版的Ubuntu中运行良好

我想将代码切换到Windows,然后再次下载所有软件包,包括ib异步版本到最新版本。

运行代码时,我得到了RuntimeWarning: coroutine 'dailyPrice' was never awaited

我不确定是否是引起问题的版本更改。我在Ubuntu机器上一切正常。

但是,错误指向异步部分。

async def dailyPrice(collection, symbol, exchange, currency, primary, semaphore):
    async with semaphore:
        TF = ifExist(collection, symbol, primary)
        if TF == False:
            stock = Stock(symbol, exchange, currency, primaryExchange= primary)
            #ib.qualifyContractsAsync(stock)
            assert await ib.reqContractDetailsAsync(stock)
            data = await ib.reqHistoricalDataAsync(
                contract=stock, endDateTime='', durationStr='2 D',
                barSizeSetting='1 day', whatToShow='TRADES',useRTH=True,
                formatDate=2)

            df = util.df(data)
            df['symbol'] = symbol

            try:
                data = pushToDB(df, primary)
            except:
                errors.append(symbol)

            return df

semaphore = asyncio.BoundedSemaphore(20)

async def main():
    return await asyncio.gather(*coros, return_exceptions=True)

if __name__ == '__main__':
    retry = 3
    while (countTicker(collection) < len(stockList)) and (retry > 0):
        ib = IB()
        #ib.setCallback('error', onError)
        ib.connect('127.0.0.1', 4002, clientId=2)

        stockList = findMissing(collection, stockList) #replace the stockList with missing Ticker
        print("Missing {} stocks' data".format(len(stockList)))

        coros = []
        for symbol, exchange in zip(stockList['IB Symbol'], stockList['Exchange']):
            coros.append(dailyPrice(collection, symbol, 'SMART', 'USD', exchange, semaphore))
        try:
            with timeout(3600, exception=RuntimeError):
                loop = asyncio.get_event_loop()
                lst_result = loop.run_until_complete(main())
        except Exception as e:
            print('logging')
            retry += 1

        ib.disconnect()

        retry -= 1
        print("Try remaining {}".format(retry))

        #restore full stockList
        stockList = pd.read_csv('stockList.csv')
        stockList.drop_duplicates(subset=['IB Symbol'], inplace=True)

end = time.time()
print(end - start)

0 个答案:

没有答案