1

我想在延时后使用 Celery 发送一些消息。用户收到消息后,触发新的状态。为此,我需要将 telebot.types.Message 对象作为 Celery 任务中的参数发送。我怎样才能正确地做到这一点?

我启动 Celery 任务的转换功能:

 def delay_message(self, event):
        celery_utils.delay_message.apply_async(kwargs={'response': self.response}, countdown=1) # self.response is telebot.types.Message

芹菜任务:

@celery.task()
def delay_message(response):
    machine = routes.DialogMachine(transitions=app.config['transitions'])
    machine.response = response
    machine.send_random_motivation_message()

send_random_motivation_message()我需要 telebot.types.Message 作为 self.response,但不能将此类型发送到 Celery 任务。

4

1 回答 1

1

我假设您无法发送它,因为它不可序列化,对吗?如果是这种情况,您唯一的选择是根据需要发送尽可能多的参数作为字典或元组,并在 Celery 任务telebot.types.Message 内部创建。

您可以尝试使用jsonpickle从腌制的 telebot.types.Message 对象生成 JSON,将其传递给您的 Celery 任务,并在任务内部使用 jsonpickle 重新创建对象。

于 2019-11-04T13:29:08.567 回答