我想从另一个脚本每 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 笔记本。 还尝试了子进程调用。
最佳答案
最简单的方法是在这些 .py 文件中定义函数。将 test.py1 更改为:
def test1():
print("this is test 1")
并将 test2.py 更改为:
def test2():
print("this is test 2")
然后将您的主要代码更改为:
from test1 import test1
from test2 import test2
def some_job():
print('hello')
test1()
test2()
关于python - 如何从另一个Python脚本重复运行一个Python脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54808429/