从单个父脚本运行多个Python脚本

时间:2016-02-19 17:56:35

标签: python subprocess multiprocessing

我想一次运行多个脚本(大约15个),我假设我可以使用子进程库来执行此操作,但我对此没有任何经验。

我正在玩这个剧本:

import os
from subprocess import *

#run child script 1
p = Popen([r'D:\python\loanrates\test\test.py', "test"], shell=True, stdin=PIPE, stdout=PIPE)
output = p.communicate()
print output

#run child script 2
p = Popen([r'D:\python\loanrates\test\test2.py', "test2"], shell=True, stdin=PIPE, stdout=PIPE)
output = p.communicate()
print output

它试图运行的每个python脚本都很简单(出于测试目的)

import time 

for i in range(50):
    print i 
    time.timer(1)

这是对的吗?这是运行每个脚本?它运行时没有错误

('', None)
('', None)
[Finished in 0.3s]

如果是这样,我需要在子脚本中更改哪些内容以便父输出?或者类似地,我需要在父级中进行chnage,以便在子级中打印打印功能?

编辑:我正在运行的脚本位于不同的文件夹中s a pastebin for the actual scripts ,每个文件夹的URL调用后缀都不同。没有函数,没有任何东西返回代码将在while True循环

上连续运行

1 个答案:

答案 0 :(得分:1)

如果您知道其他脚本的路径,可以将它们作为模块导入并运行其方法

test.py

def main():
    print "Hello, World!"

main.py

import test

test.main()

如果你想同时执行它们,请使用线程。

test1.py

def main(name):
    print "Goodbye, %s!"%name

main.py

import test,test1
from threading import Thread

Thread(target=test.main).start()
Thread(target=test1.main,args='user').start()
相关问题