【发布时间】:2019-02-21 13:37:23
【问题描述】:
我想每隔 10 秒从另一个脚本重复运行 2 个 python 脚本。
我有一个带有以下语句的 python 文件:-
test1.py
print("this is test1")
test2.py
print("this is test2")
主代码
from apscheduler.schedulers.blocking import BlockingScheduler
def some_job():
print('hello')
import test1
import test2
scheduler = BlockingScheduler()
job=scheduler.add_job(some_job, 'interval', seconds=10)
scheduler.start()
我实际上希望它打印为
hello
this is test1
this is test2
hello
this is test1
this is test2
hello
this is test1
this is test2
以此类推,每 10 秒一次。
我尝试使用 os.system('test1.py') 但它会在 pycharm 中打开文件。我正在使用 jupyter 笔记本。 还尝试了子进程调用。
【问题讨论】:
-
你试过 os.system('python test1.py') 吗?
-
函数
some_job()导入模块,但实际上并不执行它们。要使用os.system()或subprocess调用它们,您无需导入 test1 和 test2。
标签: python subprocess jupyter-notebook scheduler apscheduler