python - 如何从另一个Python脚本重复运行一个Python脚本?

标签 python subprocess jupyter-notebook scheduler apscheduler

我想从另一个脚本每 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()

我得到的结果如下 Output

我实际上希望它打印为

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/

相关文章:

python - 了解subprocess.TimeoutExpired,想在超时发生后杀死一个子进程

python - 限制从 Pandas 中的数据框显示的最大列数

python - "ImportError: No module named"尝试运行 Python 脚本时

jupyter-notebook - ipywidgets 下拉小部件 : what is the onchange event?

python - 在 Python 列表中压缩列表

python - 在tensorflow中加载多个DNN模型并多次使用它们

python - 如何打印字典?

python - 使用 Python 子进程的 ssh 的多个命令

python 子进程不能很好地使用 gsutil 复制/移动命令

python - 对象相似性 Pandas 和 Scikit Learn