首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在aiogram python中异步。从异步函数中取出

在aiogram python中异步。从异步函数中取出
EN

Stack Overflow用户
提问于 2020-08-21 17:50:37
回答 1查看 704关注 0票数 1

我用aiogram在python上写了一个程序,现在我正在重构代码。有一个问题,我无法从do_something func取出一段代码到get_task_info func:

代码语言:javascript
复制
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')

当我尝试的时候,有一个例外:

代码语言:javascript
复制
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", line 117, in notify
    response = await handler_obj.handler(*args, **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, memo)
  File "C:\Users\HP\miniconda3\lib\copy.py", line 169, in deepcopy
    rv = reductor(4)
TypeError: can't pickle coroutine objects

Process finished with exit code -1

我该怎么办?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-21 18:24:55

get_task_info是异步函数,您应该执行异步调用来获得结果添加等待操作符。

取代

代码语言:javascript
复制
task_info = get_task_info(task_info, message)

代码语言:javascript
复制
task_info = await get_task_info(task_info, message)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63527763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档