Python异步/不和.py具有任务的循环出口已销毁,但它处于挂起状态

2024-09-30 12:32:27 发布

您现在位置:Python中文网/ 问答频道 /正文

与我的问题相关的代码:

不和谐bot.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 {1}x 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)

主.py

^{pr2}$

错误和回溯:

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中断。在


Tags: inpytasklibbotlineuserscommands
1条回答
网友
1楼 · 发布于 2024-09-30 12:32:27

discord客户机显然每分钟都需要控制,而您的get_sells函数阻塞的时间远远不止这些。在

这是因为您将同步bittrex客户机与异步库(discord)混合在一起。您将需要对两者使用相同的范例:discord客户机基于asyncio,因此您需要为bitrex找到一个基于asyncio的库(或编写一个)。在

这个新库应该公开一些与您正在使用的函数类似的协同程序。因此,您将调用sells = await bittrex_aio.get_orderbook(…),而不是sells = bittrex.get_orderbook(…)def get_sells(…)将成为一个协程async def get_sells(…)。在

这样,您就可以将bot更改为使用await main.get_sells,这意味着在您等待get_sells的结果时,discord客户端将能够控制。在

在这里我并不是有意深入讨论asyncio的具体内容,但是阅读一下它可能会有所帮助,例如:https://docs.python.org/3/library/asyncio.html

相关问题 更多 >

    热门问题