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()
任何幫助表示讚賞。