2017-07-12 23 views
0

私の問題に関連し、関連するコードを保留していると:Pythonのasyncio/discord.py - ループが終了し、タスクが破壊されたが、それは

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 {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) 

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秒ごとに1回の制御を必要とするため、問題があると考えています。私の関数は〜70秒ほどかかるので、asyncioはブレークします。

答えて

0

discordクライアントは明らかに分単位で制御する必要があり、get_sells機能はそれ以上の機能をブロックしています。

これは、同期bittrexクライアントとasyncioライブラリ(discord)を混在させるためです。あなたは両方とも同じパラダイムを使用する必要があります:discordクライアントはasyncioに基づいているため、bittrex用のasyncioベースのライブラリを見つける(または書き込む)必要があります。

この新しいライブラリは、使用している関数と同様のコルーチンを公開する必要があります。だから、sells = bittrex.get_orderbook(…)def get_sells(…)の代わりにsells = await bittrex_aio.get_orderbook(…)と呼ぶとコルーチンasync def get_sells(…)になります。これにより

、あなたは不和のクライアントはあなたがget_sellsの結果を待っているながら制御を取ることができるようになります意味await main.get_sellsを使用するようにボットを変更することができます。

私は、例えば、ここで、ここでの目的上asyncioの特定に入るが、それはおそらく役立つだろうについて読んでいない午前:https://docs.python.org/3/library/asyncio.html

関連する問題