错误"任务被破坏但是它正在等待"使用异步。 (Python 3.4)

时间:2016-04-15 02:38:53

标签: python asynchronous

我最近使用discord.py为Discord服务器开发了一个机器人,但我在使用异步时遇到了一些麻烦。僵尸程序运行一段时间,但一段时间后我收到以下错误:

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

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

经过一段时间修改代码后,我发现请求被阻止了,所以我调整了代码以使用aiohttp代替到目前为止没有错误。