如何解决在Aiogram Python中异步处理从异步功能中退出
我使用aiogram在python上编写了一个程序,现在我正在重构代码。有一个问题,我无法从do_something func到get_task_info func提取一段代码:
async def get_task_info(task_info,message: types.Message):
if message.content_type == 'text':
task_info.append(message.text)
elif message.content_type == 'photo':
task_info.extend([str(message.caption),await message.photo[-1].get_url()])
elif message.content_type == 'document':
task_info.extend([str(message.caption),await message.document.get_url()])
return task_info
@dp.message_handler(state=Order.some_state)
async def do_something(message: types.Message,state: FSMContext):
data = await state.get_data()
task_info = data.get('task_info',list())
task_info = get_task_info(task_info,message)
await state.update_data(task_info=task_info)
await message.answer('Done')
当我尝试这样做时,有一个例外:
ERROR:asyncio:Task exception was never retrieved
future: <Task finished coro=<Dispatcher._process_polling_updates() done,defined at C:\Users\HP\miniconda3\lib\site-packages\aiogram\dispatcher\dispatcher.py:331> exception=TypeError("can't pickle coroutine objects")>
Traceback (most recent call last):
File "C:\Users\HP\miniconda3\lib\site-packages\aiogram\dispatcher\dispatcher.py",line 339,in _process_polling_updates
for responses in itertools.chain.from_iterable(await self.process_updates(updates,fast)):
File "C:\Users\HP\miniconda3\lib\site-packages\aiogram\dispatcher\dispatcher.py",line 194,in process_updates
return await asyncio.gather(*tasks)
File "C:\Users\HP\miniconda3\lib\site-packages\aiogram\dispatcher\handler.py",line 117,in notify
response = await handler_obj.handler(*args,**partial_data)
File "C:\Users\HP\miniconda3\lib\site-packages\aiogram\dispatcher\dispatcher.py",line 214,in process_update
return await self.message_handlers.notify(update.message)
File "C:\Users\HP\miniconda3\lib\site-packages\aiogram\dispatcher\handler.py",**partial_data)
File "F:\Telegram\handlers\order.py",line 110,in commit_task
data = await state.get_data()
File "C:\Users\HP\miniconda3\lib\site-packages\aiogram\dispatcher\storage.py",line 298,in get_data
return await self.storage.get_data(chat=self.chat,user=self.user,default=default)
File "C:\Users\HP\miniconda3\lib\site-packages\aiogram\contrib\fsm_storage\memory.py",line 45,in get_data
return copy.deepcopy(self.data[chat][user]['data'])
File "C:\Users\HP\miniconda3\lib\copy.py",line 150,in deepcopy
y = copier(x,memo)
File "C:\Users\HP\miniconda3\lib\copy.py",line 240,in _deepcopy_dict
y[deepcopy(key,memo)] = deepcopy(value,line 169,in deepcopy
rv = reductor(4)
TypeError: can't pickle coroutine objects
Process finished with exit code -1
我该怎么办?
解决方法
get_task_info是异步函数,您应该进行异步调用以获取添加等待操作符的结果。
替换
body['rateRequestPayments'][<!WHICH DATA SET YOU WANT TO ACCESS>][<!VALUE YOU WANT TO FIND>]
至
task_info = get_task_info(task_info,message)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。