Python 如期付诸东流
我试图安排任务的执行,我不关心任务何时完成以及如何完成。它是Python3.5,在其他地方没有安装或想要安装其他异步包,只有“asyncio” 有一个很好的包“时间表”,它可以很好地安排时间:Python 如期付诸东流,python,python-asyncio,Python,Python Asyncio,我试图安排任务的执行,我不关心任务何时完成以及如何完成。它是Python3.5,在其他地方没有安装或想要安装其他异步包,只有“asyncio” 有一个很好的包“时间表”,它可以很好地安排时间: import schedule schedule.every(5).seconds.do(call_my_remote_url) while 1: schedule.run_pending() time.sleep(1) 有一个火灾和遗忘的代码,它的工作之一: def fire_an
import schedule
schedule.every(5).seconds.do(call_my_remote_url)
while 1:
schedule.run_pending()
time.sleep(1)
有一个火灾和遗忘的代码,它的工作之一:
def fire_and_forget(task, *args, **kwargs):
loop = asyncio.get_event_loop()
if callable(task):
return loop.run_in_executor(None, task, *args, **kwargs)
else:
raise TypeError('Task must be a callable')
然而,当我把它放在一起时:
import schedule
import time
import datetime
import asyncio
def call_my_remote_url():
# Do stuff, completion of which I don't care. For example
print("started @ "+str(datetime.datetime.now().time()))
time.sleep(5)
print("completed @ " + str(datetime.datetime.now().time()))
def fire_and_forget(task, *args, **kwargs):
loop = asyncio.get_event_loop()
if callable(task):
return loop.run_in_executor(None, task, *args, **kwargs)
else:
raise TypeError('Task must be a callable')
schedule.every(5).seconds.do(fire_and_forget(call_my_remote_url))
while 1:
schedule.run_pending()
time.sleep(1)
它失败得很惨:
(env) $ python3 example_fire_and_forget_with_scheduler.py
started @ 12:19:04.901547
Traceback (most recent call last):
File "example_fire_and_forget_with_scheduler.py", line 21, in <module>
schedule.every(5).seconds.do(fire_and_forget(call_my_remote_url))
File "/Users/user/Projects/env/lib/python3.5/site-packages/schedule/__init__.py", line 352, in do
self.job_func = functools.partial(job_func, *args, **kwargs)
TypeError: the first argument must be callable
completed @ 12:19:09.906020
(env)$
(env)$python3示例\u fire\u和\u forget\u with_scheduler.py
于12:19:04.901547开始
回溯(最近一次呼叫最后一次):
文件“example\u fire\u and\u forget\u with_scheduler.py”,第21行,在
计划。每(5)秒。执行(启动和忘记(调用我的远程url))
do中的文件“/Users/user/Projects/env/lib/python3.5/site packages/schedule/_init__.py”,第352行
self.job_func=functools.partial(job_func,*args,**kwargs)
TypeError:第一个参数必须是可调用的
于12:19:09.906020时完成
(环境)美元
似乎“调度”模块不高兴,btu我不明白为什么以及如何修复它。有什么想法或简单的选择吗?
(请注意,它不仅是异步的,而且必须按计划启动并忘记)您正在以一种它不喜欢的方式将参数传递给
do
。稍微简化一下问题,以下方法似乎有效:
import schedule
import time
import datetime
def call_my_remote_url():
# Do stuff, completion of which I don't care. For example
print("started @ "+str(datetime.datetime.now().time()))
time.sleep(5)
print("completed @ " + str(datetime.datetime.now().time()))
def fire_and_forget(task, *args, **kwargs):
if callable(task):
return task()
else:
raise TypeError('Task must be a callable')
schedule.every(5).seconds.do(fire_and_forget, call_my_remote_url)
while 1:
schedule.run_pending()
time.sleep(1)
因此,
fire\u和{u forget
的参数被传递到do
。我似乎在文档中找不到这方面的详细信息,但这是由所暗示的。您是否尝试过只调用fire\u并忘记(call\u my\u remote\u url())
,而不进行任何安排?是的。它也不起作用:文件“example\u fire\u and\u forget\u with_scheduler.py”,在schedule.中的第21行,每隔(5)秒.do(fire\u and\u forget(call\u my\u remote\u url())文件“example\u fire\u and\u forget\u with_scheduler.py”,在fire\u and\u forget raise TypeError(“任务必须是可调用的”)TypeError:任务必须是可调用的