python脚本在没有线程的情况下并发执行

时间:2016-01-20 12:02:45

标签: python python-multithreading python-multiprocessing

如何在不使用其他python脚本的Threads和Multiprocessing的情况下,使用不同的PID同时多次执行单个python脚本?

我需要从每次执行中获得结果

我尝试了多处理模块简单程序,我得到了AttributeError: 'module' object has no attribute 'f',它需要在Linux和Windows上运行。以前的帖子解决方案不适合我

在使用之前的帖子进行验证后,我再次发布了此内容。

2 个答案:

答案 0 :(得分:1)

不同的PID意味着您需要不同的流程。不使用multiprocessing您可以使用subprocess模块开始另一个流程,并通过stdout获取结果:

#!/usr/bin/env python
# coding: utf8
from __future__ import absolute_import, division, print_function
import pickle
import sys
from subprocess import PIPE, Popen


def main():
    processes = [
        Popen([sys.executable, 'test.py'], stdout=PIPE)
        for _ in xrange(5)
    ]
    results = [pickle.loads(p.stdout.read()) for p in processes]
    for process in processes:
        process.wait()
    print(results)


if __name__ == '__main__':
    main()

test.py需要将pickle序列化的结果写入其stdout

答案 1 :(得分:0)

愿这有帮助吗?

test.py :(仅用于演示)

import time, os, datetime, fcntl

with open("output.txt", "a") as g:
    fcntl.flock(g, fcntl.LOCK_EX)
    g.write( "PID [" + str(os.getpid()) + "]," + str(datetime.datetime.now()) + "\n" )
    fcntl.flock(g, fcntl.LOCK_UN)


time.sleep(30)

with open("output.txt", "a") as g:
    fcntl.flock(g, fcntl.LOCK_EX)
    g.write( "PID [" + str(os.getpid()) + "]," + str(datetime.datetime.now()) + "\n" )
fcntl.flock(g, fcntl.LOCK_UN)

多次运行:

c@chen:~/src$ python test2.py &
[1] 29265
c@chen:~/src$ python test2.py &
[2] 29266
c@chen:~/src$ python test2.py &
[3] 29268
c@chen:~/src$ python test2.py &
[4] 29269
c@chen:~/src$ vim test2.py 
[1]   Done                    python test2.py
[2]   Done                    python test2.py
[3]-  Done                    python test2.py
[4]+  Done                    python test2.py

输出:

c@chen:~/src$ tail -f -n 100 output.txt 
PID [29265],2016-01-20 16:28:20.373244
PID [29266],2016-01-20 16:28:21.068946
PID [29268],2016-01-20 16:28:21.911043
PID [29269],2016-01-20 16:28:22.547805
PID [29265],2016-01-20 16:28:50.403474
PID [29266],2016-01-20 16:28:51.075268
PID [29268],2016-01-20 16:28:51.914001
PID [29269],2016-01-20 16:28:52.564706