Python 调度程序不是 运行 在导入中调度的函数
Python scheduler not running a function scheduled in an import
问题
当这段代码:
# main.py - this file gets run directly.
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)
print("before definition")
def do_something():
print("do_something is being executed.")
scheduler.enter(1, 5, do_something)
print("post definition")
scheduler.enter(1, 5, do_something)
# Do something, so that the program does not terminate.
do_not_terminate()
在被调用的文件中 (main.py) 它 运行 如预期的那样(do_something 函数是 运行 每秒。),产生以下输出:
before definition
post definition
do_something is being executed.
do_something is being executed.
do_something is being executed.
do_something is being executed.
do_something is being executed.
do_something is being executed.
...
但是:
当上面的块被放入一个单独的文件中时(someimport.py)并且 someimport.py 被导入在 main.py 中,do_something 函数不再执行。 [代码如下所示:]
# someimport.py - this file gets imported in main.py
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)
print("before definition")
def do_something():
print("do_something is being executed.")
scheduler.enter(1, 5, do_something)
print("post definition")
scheduler.enter(1, 5, do_something)
# main.py - this file get's run directly
import someimport
# Do something, so that the program does not terminate.
do_not_terminate()
仅产生以下输出(当然不会出现错误消息):
before definition
post definition
我已经尝试过以下方法:
1. main.py中的import someimport
改为from someimport import *
正如预期的那样,这并没有改变任何东西。
2.将第一个scheduler.enter调用放到一个单独的函数中(运行),导入后调用:
# someimport.py - this file gets imported in main.py
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)
print("before definition")
def do_something():
print("do_something is being executed.")
scheduler.enter(1, 5, do_something)
print("post definition")
def run():
scheduler.enter(1, 5, do_something)
# main.py - this file get's run directly
import someimport
someimport.run()
# Do something, so that the program does not terminate.
do_not_terminate()
这并没有改变任何东西。
3. 在main.py中导入sched和time。
不出所料,这也没有任何效果。
有什么方法可以在 someimport.py 中安排 do_something 功能,而无需第一个 scheduler.enter 在 main.py 文件中。
这是我试图避免的事情(因为在实际项目中有大约 100 个这样的计划任务,我正在尝试清理主文件)。
以上所有代码均已在 GNU/Linux 下的 python 3.7.3 上进行测试。
非常感谢!
在将 scheduler
实例导入 main.py.
后,我只能通过调用 run()
来使您的单独模块示例正常工作
# main.py - this file get's run directly
import someimport
someimport.scheduler.run() # Blocks until all events finished
产生:
before definition
post definition
do_something is being executed.
do_something is being executed.
根据 the docs,必须调用 scheduler.run()
才能 运行 任何计划任务。 scheduler.enter()
方法本身只会安排事件,而不是 运行 它们。
问题
当这段代码:
# main.py - this file gets run directly.
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)
print("before definition")
def do_something():
print("do_something is being executed.")
scheduler.enter(1, 5, do_something)
print("post definition")
scheduler.enter(1, 5, do_something)
# Do something, so that the program does not terminate.
do_not_terminate()
在被调用的文件中 (main.py) 它 运行 如预期的那样(do_something 函数是 运行 每秒。),产生以下输出:
before definition
post definition
do_something is being executed.
do_something is being executed.
do_something is being executed.
do_something is being executed.
do_something is being executed.
do_something is being executed.
...
但是:
当上面的块被放入一个单独的文件中时(someimport.py)并且 someimport.py 被导入在 main.py 中,do_something 函数不再执行。 [代码如下所示:]
# someimport.py - this file gets imported in main.py
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)
print("before definition")
def do_something():
print("do_something is being executed.")
scheduler.enter(1, 5, do_something)
print("post definition")
scheduler.enter(1, 5, do_something)
# main.py - this file get's run directly
import someimport
# Do something, so that the program does not terminate.
do_not_terminate()
仅产生以下输出(当然不会出现错误消息):
before definition
post definition
我已经尝试过以下方法:
1. main.py中的import someimport
改为from someimport import *
正如预期的那样,这并没有改变任何东西。
2.将第一个scheduler.enter调用放到一个单独的函数中(运行),导入后调用:# someimport.py - this file gets imported in main.py
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)
print("before definition")
def do_something():
print("do_something is being executed.")
scheduler.enter(1, 5, do_something)
print("post definition")
def run():
scheduler.enter(1, 5, do_something)
# main.py - this file get's run directly
import someimport
someimport.run()
# Do something, so that the program does not terminate.
do_not_terminate()
这并没有改变任何东西。
3. 在main.py中导入sched和time。不出所料,这也没有任何效果。
有什么方法可以在 someimport.py 中安排 do_something 功能,而无需第一个 scheduler.enter 在 main.py 文件中。 这是我试图避免的事情(因为在实际项目中有大约 100 个这样的计划任务,我正在尝试清理主文件)。
以上所有代码均已在 GNU/Linux 下的 python 3.7.3 上进行测试。
非常感谢!
在将 scheduler
实例导入 main.py.
run()
来使您的单独模块示例正常工作
# main.py - this file get's run directly
import someimport
someimport.scheduler.run() # Blocks until all events finished
产生:
before definition
post definition
do_something is being executed.
do_something is being executed.
根据 the docs,必须调用 scheduler.run()
才能 运行 任何计划任务。 scheduler.enter()
方法本身只会安排事件,而不是 运行 它们。