2016-04-15 166 views
2

我最近开发了一个使用discord.py的Discord服务器的bot,但我在使用异步时遇到了一些麻烦。僵尸程序运行就好了一段时间,但一段时间后,我得到了以下错误:错误“任务已被破坏,但正在等待”使用异步。 (Python 3.4)

Task was destroyed but it is pending! 
task: <Task pending coro=<my_background_task() running at setup.py:431> wait_for=<Future pending cb=[Task._wakeup()]>> 

下面的代码的相关部分:

@client.async_event 
def my_background_task(): 
    yield from client.wait_until_ready() 
    channel = discord.Object(id="121156929409122306") 
    while not client.is_closed: 
    # STARTS EQ BOT # 
    r = requests.get('https://api.pushbullet.com/v2/channel-info?tag=pso2') 
    query = json.loads(r.text) 

    eq = query['recent_pushes'][0]['body'] 

    with open('last_eq.json', encoding="utf8") as in_f: 
     last_eq = json.load(in_f) 

    if last_eq['eq'][-1] != eq: 
     last_eq['eq'].append(eq) 

     with open('last_eq.json', 'w') as out_f: 
     json.dump(last_eq, out_f) 

     pso2_channel = discord.Object("148846969861701632") 
     yield from client.send_message(pso2_channel, '**EQ ALERT:** %s' % eq) 

    # STARTS BUMPED BOT # 
    try: 
     d = feedparser.parse('http://www.bumped.org/psublog/feed') 
     articleTitle = d['entries'][0]['title'] 
     articleLink = d['entries'][0]['link'] 

     with open('last_article.json', encoding="utf8") as in_f: 
      last_article = json.load(in_f) 

     if last_article['article'][-1] != articleTitle: 
      last_article['article'].append(articleTitle) 

      with open('last_article.json', 'w') as out_f: 
       json.dump(last_article, out_f) 

      pso2_channel = discord.Object("148846969861701632") 
      yield from client.send_message(pso2_channel, '**NEW BUMPED ARTICLE:** %s\n%s' % (articleTitle, articleLink)) 
    except: 
     pass 

    yield from asyncio.sleep(60) # task runs every 60 seconds 

loop = asyncio.get_event_loop() 

try: 
    loop.create_task(my_background_task()) 
    loop.run_until_complete(client.login('email', 'password')) 
    loop.run_until_complete(client.connect()) 
except Exception: 
    print(Exception) 
finally: 
    loop.close() 

任何帮助表示赞赏。

回答

0

经过一段时间的代码修补,我发现请求被阻止,所以我改编的代码使用aiohttp,而不是错误到目前为止。