Python asyncio/discord.py - 循环退出,任务被破坏,但它处于待处理状态
Posted
技术标签:
【中文标题】Python asyncio/discord.py - 循环退出,任务被破坏,但它处于待处理状态【英文标题】:Python asyncio/discord.py - Loop exits with Task was destroyed but it is pending 【发布时间】:2017-12-17 08:36:29 【问题描述】:与我的问题相关的代码:
discordbot.py
import discord, main, websockets
from discord.ext import commands
TOKEN = ''
description = '''Description'''
bot = commands.Bot(command_prefix='!', description=description)
@bot.event
async def on_ready():
print('----------------------------')
print('Connected!')
print('Logged in as: 0'.format(bot.user.name))
print('Bot ID: 0'.format(bot.user.id))
print('----------------------------')
@bot.command()
async def wall(coin, desired_multiplier):
try:
desired_multiplier = float(desired_multiplier)
except:
await bot.say("That's not a percent!")
desired_multiplier = float("0:.1f".format(desired_multiplier))
try:
if desired_multiplier <= 2.0:
#await bot.say('Calculating...')
volume, rate, last_price = main.get_sells(coin, desired_multiplier)
total_btc = sum(volume)
total_btc = float("0:.3f".format(total_btc))
await bot.say('Success')
print('Total of 0 BTC to reach a 1x multiplier for 2'.format(total_btc, desired_multiplier, coin.upper()))
else:
await bot.say('Please use a multiplier under 2x, apparently I can\'t handle more than that.')
except:
await bot.say('Error: Please make sure the coin is registered on Bittrex!')
bot.run(TOKEN)
main.py
from bittrex import Bittrex
import requests
bittrex = Bittrex('', '')
BUY_ORDERBOOK = 'buy'
SELL_ORDERBOOK = 'sell'
BOTH_ORDERBOOK = 'both'
def total_btc(quantity, rate):
total = rate * quantity
return total
def get_sells(coin, desired_multiplier):
volume = []
sells = bittrex.get_orderbook('BTC-0'.format(coin), SELL_ORDERBOOK)
if sells['success'] is False:
print(sells['message'], '#########################################################')
for sell in range(10000):
order = sells['result'][sell]
quantity = order['Quantity']
rate = order['Rate']
total = total_btc(quantity, rate)
volume.append(total)
last_price = bittrex.get_ticker('BTC-0'.format(coin))['result']['Last']
multiplier = rate / last_price
multiplier = float("0:.1f".format(multiplier))
print('0 | 1'.format(sell, multiplier))
if multiplier != desired_multiplier:
continue
else:
return volume, rate, last_price
错误和回溯:
Task was destroyed but it is pending!
task: <Task pending coro=<Client._run_event() running at C:\Users\logicmn\D
ocuments\discordenv\lib\site-packages\discord\client.py:307> wait_for=<Future pe
nding cb=[BaseSelectorEventLoop._sock_connect_done(704)(), <TaskWakeupMethWrappe
r object at 0x0000000003CE2258>()]>>
Exception ignored in: <generator object Bot.on_message at 0x0000000003CDC048>
Traceback (most recent call last):
File "C:\Users\logicmn\Documents\discordenv\lib\site-packages\discord\ext
\commands\bot.py", line 857, in on_message
yield from self.process_commands(message)
File "C:\Users\logicmn\Documents\discordenv\lib\site-packages\discord\ext
\commands\bot.py", line 848, in process_commands
ctx.command.dispatch_error(e, ctx)
File "C:\Users\logicmn\Documents\discordenv\lib\site-packages\discord\ext
\commands\core.py", line 164, in dispatch_error
ctx.bot.dispatch('command_error', error, ctx)
File "C:\Users\logicmn\Documents\discordenv\lib\site-packages\discord\ext
\commands\bot.py", line 262, in dispatch
super().dispatch(event_name, *args, **kwargs)
File "C:\Users\logicmn\Documents\discordenv\lib\site-packages\discord\cli
ent.py", line 325, in dispatch
compat.create_task(self._run_event(method, *args, **kwargs), loop=self.loop)
File "c:\users\logicmn\appdata\local\continuum\anaconda3\Lib\asyncio\task
s.py", line 512, in ensure_future
task = loop.create_task(coro_or_future)
File "c:\users\logicmn\appdata\local\continuum\anaconda3\Lib\asyncio\base
_events.py", line 282, in create_task
self._check_closed()
File "c:\users\logicmn\appdata\local\continuum\anaconda3\Lib\asyncio\base
_events.py", line 357, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
This seems relevant 但我尝试了该解决方案但无法使其正常工作。我确实相信这个问题是因为不和谐客户端模块需要每 60 秒控制一次。由于我的函数 get_sells()
需要大约 70 秒,因此 asyncio 会中断。
【问题讨论】:
【参考方案1】:discord 客户端显然需要每分钟左右进行一次控制,而您的 get_sells
函数的阻塞不止于此。
这是因为您将同步 bittrex 客户端与异步库 (discord) 混合在一起。您需要对两者使用相同的范例:discord 客户端基于 asyncio,因此您需要为 bittrex 找到一个基于 asyncio 的库(或编写一个)。
这个新库应该公开一些与您正在使用的函数类似的协程。所以你会调用sells = await bittrex_aio.get_orderbook(…)
,而不是sells = bittrex.get_orderbook(…)
,def get_sells(…)
会变成协程async def get_sells(…)
。
这样,您将能够将您的机器人更改为使用await main.get_sells
,这意味着不和谐客户端将能够控制您正在等待get_sells
的结果。
我不是故意在这里详细介绍 asyncio,但阅读它可能会有所帮助,例如:https://docs.python.org/3/library/asyncio.html
【讨论】:
以上是关于Python asyncio/discord.py - 循环退出,任务被破坏,但它处于待处理状态的主要内容,如果未能解决你的问题,请参考以下文章