1

我正在制作一个脚本来运行多个线程实例。

运行超过 3 个并发线程集时,我不断收到错误,它们主要与管道有关。如何杀死多个单独的进程?有一个更好的方法吗?

from multiprocessing import Process, Pipe
from threading import Thread
import time


alive = {'subAlive': True,  'testAlive': True};


def control_listener(conn, threadAlive): #listens for kill from main
    global alive
    while True:
          data = conn.recv()
           if data == "kill":
               print "Killing"
               alive[threadAlive] = False; #value for kill
               print "testListner alive %s" % threadAlive, alive[threadAlive];
               break

def subprocess(conn, threadNum, threadAlive):
    t = Thread(target=control_listener, args=(conn, threadAlive))
    count = 0
    threadVal = threadNum 
    t.start()
    run = alive['subAlive'];
    while run == True:
          print "Thread %d Run number = %d" % (threadVal, count), alive['subAlive'];
          count = count + 1
          run = alive['subAlive']; 


def testprocess(conn, threadNum, threadAlive):
    t = Thread(target=control_listener, args=(conn, threadAlive))
    count = 0
    threadVal = threadNum 
    t.start()
    run = alive['testAlive'];
    while run == True:
          print "This is a different thread %d Run = %d" % (threadVal, count)
          count = count + 1
          run = alive['testAlive'];

sub_parent, sub_child = Pipe()
test_parent, test_child = Pipe()
runNum = int(raw_input("Enter a number: ")) 
threadNum = int(raw_input("Enter number of threads: "))


print "Starting threads"

for i in range(threadNum):
    p = Process(target=subprocess, args=(sub_child, i, 'subAlive'))
    p.start()

print "Subprocess started"

for i in range(threadNum): 
    p2 = Process(target=testprocess, args=(test_child, i, 'testAlive'))
    p2.start()

print "Testproccess started"

print "Starting run"

time.sleep(runNum) 

print "Terminating Subprocess run"
for i in range(threadNum):
    sub_parent.send("kill") #sends kill to listener
    print "Testprocess termination alive", alive['subAlive'];

print "Terminating Testprocess run"
for i in range(threadNum):
    test_parent.send("kill") #sends kill to listener
    print "Testprocess termination alive", alive['subAlive'];

p.join()
p2.join()

如果我使用超过 2 个线程运行它,我会收到随机错误,例如

Exception in thread Thread-1:^M
Traceback (most recent call last):^M
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner^M
  self.run()^M
  File "/usr/lib/python2.7/threading.py", line 504, in run^M
   self.__target(*self.__args, **self.__kwargs)^M
  File "multiprocessDemo.py", line 28, in control_listener^M
   data = conn.recv()^M
 EOFError

或这个

Traceback (most recent call last):^M
 File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner^M
self.run()^M
 File "/usr/lib/python2.7/threading.py", line 504, in run^M
self.__target(*self.__args, **self.__kwargs)^M
 File "multiprocessDemo.py", line 28, in control_listener^M
data = conn.recv()^M
MemoryError

当消息传递时,它们偶尔会发生,然后两个线程中的一个将停止,但另一个将继续运行。

我希望能够在进行多件事情的情况下运行它,比如总共 16 个并发线程,它们是几种不同类型中的一种。我真正需要做的就是停止并可靠地启动它们。我不需要同步作业,也不需要复杂的进程间通信。有什么建议么?我可以看看的例子?

4

0 回答 0