Python多处理应用程序中的内存错误
我正在制作一个脚本来运行多个线程实例 当运行3组以上的并发线程时,我经常会遇到错误,这些错误主要与管道有关。如何杀死多个单独的进程?有更好的方法吗Python多处理应用程序中的内存错误,python,memory,pipe,multiprocess,Python,Memory,Pipe,Multiprocess,我正在制作一个脚本来运行多个线程实例 当运行3组以上的并发线程时,我经常会遇到错误,这些错误主要与管道有关。如何杀死多个单独的进程?有更好的方法吗 from multiprocessing import Process, Pipe from threading import Thread import time alive = {'subAlive': True, 'testAlive': True}; def control_listener(conn, threadAlive):
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()
如果我用两个以上的线程运行它,就会出现如下随机错误
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个并发线程,总共是几种不同类型中的一种。我真正需要做的就是停下来,可靠地启动它们。我不需要同步作业,也不需要复杂的进程间通信。有什么建议吗?我可以看一些例子吗