在restplus中发送响应2小时后需要执行一个方法

2024-05-26 07:47:57 发布

您现在位置:Python中文网/ 问答频道 /正文

在电子商务应用程序中,我的要求是,下单后两小时后向卖家发送通知(执行方法)。你知道吗

2小时后自动执行方法的最佳方式是什么。?你知道吗


Tags: 方法应用程序方式电子商务小时下单卖家
3条回答

这将适用于python3。你知道吗

如果您熟悉asyncio,可以使用event loopcall\u later函数。你知道吗

Event loop

loop.call_later(delay, callback, *args, context=None)

Schedule callback to be called after the given delay number of seconds (can be either an int or a float).

An instance of asyncio.TimerHandle is returned which can be used to cancel the callback.

callback will be called exactly once. If two callbacks are scheduled for exactly the same time, the order in which they are called is undefined.

The optional positional args will be passed to the callback when it is called. If you want the callback to be called with keyword arguments use functools.partial().

An optional keyword-only context argument allows specifying a custom contextvars.Context for the callback to run in. The current context is used when no context is provided.

Changed in version 3.7: The context keyword-only parameter was added. See PEP 567 for more details.

Changed in version 3.8: In Python 3.7 and earlier with the default event loop implementation, the delay could not exceed one day. This has been fixed in Python 3.8.

示例(Source)

import asyncio
import datetime

def display_date(end_time, loop):
    print(datetime.datetime.now())
    if (loop.time() + 1.0) < end_time:
        loop.call_later(1, display_date, end_time, loop)
    else:
        loop.stop()

loop = asyncio.get_event_loop()

# Schedule the first call to display_date()
end_time = loop.time() + 5.0
loop.call_soon(display_date, end_time, loop)

# Blocking call interrupted by loop.stop()
try:
    loop.run_forever()
finally:
    loop.close()

您可以使用芹菜在一定的延迟后运行任务。你知道吗

你可以在这里了解更多

http://docs.celeryproject.org/en/master/userguide/calling.html#eta-and-countdown

在我的生产代码中,我更喜欢APScheduler而不是芹菜,因为它提供了许多关于运行具有特定延迟或在特定日期的作业的设置(因此您只需将timedelta(hours=2)添加到datetime.utcnow()

看看吧: https://apscheduler.readthedocs.io/en/stable/

相关问题 更多 >