关于多线程:Python线程化多个bash子进程?

Python threading multiple bash subprocesses?

如何使用线程和子进程模块生成并行的bash进程?当我启动线程时,这里的第一个答案是:如何在Python中使用线程?,bash进程按顺序运行,而不是并行运行。


你不需要运行在并行线程的过程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from subprocess import Popen

commands = [
    'date; ls -l; sleep 1; date',
    'date; sleep 5; date',
    'date; df -h; sleep 3; date',
    'date; hostname; sleep 2; date',
    'date; uname -a; date',
]
# run in parallel
processes = [Popen(cmd, shell=True) for cmd in commands]
# do other things here..
# wait for completion
for p in processes: p.wait()

限制使用的并发数,你可以使用命令multiprocessing.dummy.Pool线程和提供相同的使用方法是:multiprocessing.Pool接口

1
2
3
4
5
6
7
8
from functools import partial
from multiprocessing.dummy import Pool
from subprocess import call

pool = Pool(2) # two concurrent commands at a time
for i, returncode in enumerate(pool.imap(partial(call, shell=True), commands)):
    if returncode != 0:
       print("%d command failed: %d" % (i, returncode))

这个答案demonstrates各种技术限制的并发数:它multiprocessing表演过程。concurrent.futures +队列,线程池为基础的解决方案。

你可以限制孩子数量的并发进程不使用线程池/流程:

1
2
3
4
5
6
7
8
9
10
11
12
13
from subprocess import Popen
from itertools import islice

max_workers = 2  # no more than 2 concurrent processes
processes = (Popen(cmd, shell=True) for cmd in commands)
running_processes = list(islice(processes, max_workers))  # start new processes
while running_processes:
    for i, process in enumerate(running_processes):
        if process.poll() is not None:  # the process has finished
            running_processes[i] = next(processes, None)  # start new process
            if running_processes[i] is None: # no new processes
                del running_processes[i]
                break

在线系统可以避免繁忙,你在线os.waitpid(-1, 0)环和块的任何儿童的过程中,对于出口。


简单的线程的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import threading
import Queue
import commands
import time

# thread class to run a command
class ExampleThread(threading.Thread):
    def __init__(self, cmd, queue):
        threading.Thread.__init__(self)
        self.cmd = cmd
        self.queue = queue

    def run(self):
        # execute the command, queue the result
        (status, output) = commands.getstatusoutput(self.cmd)
        self.queue.put((self.cmd, output, status))

# queue where results are placed
result_queue = Queue.Queue()

# define the commands to be run in parallel, run them
cmds = ['date; ls -l; sleep 1; date',
        'date; sleep 5; date',
        'date; df -h; sleep 3; date',
        'date; hostname; sleep 2; date',
        'date; uname -a; date',
       ]
for cmd in cmds:
    thread = ExampleThread(cmd, result_queue)
    thread.start()

# print results as we get them
while threading.active_count() > 1 or not result_queue.empty():
    while not result_queue.empty():
        (cmd, output, status) = result_queue.get()
        print('%s:' % cmd)
        print(output)
        print('='*60)
    time.sleep(1)

请注意,有一些更好的方法做这个,但是这不是一个太复杂了。一个例子使用的线程,为每个命令。当你开始蠕变到复杂的事物中要使用的有限数量的线程类的命令到一个句柄数。这些更先进的技术不太复杂了,你似乎有一个线程抓取一次)的基础。你有一次和multiprocessing变得更容易把在线的那些技术。


这是因为它是应该的,你想的东西,但这是不multithreadind国有企业multiprocessing堆栈页