我遇到了这个多进程脚本的问题,我在此处找到它之后对其进行了建模 http://broadcast.oreilly.com/2009/04/pymotw-multiprocessing-part-2.html
class test_imports:#Test classes remove
def import_1(self, control_queue, thread_number):
print ("Import_1 number %d started") % thread_number
run = True
count = 1
while run:
alive = control_queue.get()
if alive == 't1kill':
print ("Killing thread type 1 number %d") % thread_number
run = False
break
print ("Thread type 1 number %d run count %d") % (thread_number, count)
count = count + 1
def worker_generator(control_queue, threadName, runNum):
if threadName == 'one':
print ("Starting import_1 number %d") % runNum
p = Process(target=test_import.import_1, args=(control_queue, runNum))
p.start()
if __name__ == '__main__':
# Establish communication queues
control = multiprocessing.Queue()
runNum = int(raw_input("Enter a number: "))
threadNum = int(raw_input("Enter number of threads: "))
threadName = raw_input("Enter number: ")
thread_Count = 0
print ("Starting threads")
for i in range(threadNum):
worker_generator(control, threadName, i)
thread_Count = thread_Count + 1
time.sleep(runNum)#let threads do their thing
print ("Terminating threads")
for i in range(thread_Count):
control.put("t1kill")
control.put("t2kill")
这是我运行时遇到的错误:
Traceback (most recent call last):
File "multiQueue.py", line 62, in <module>
worker_generator(control, threadName, i)
File "multiQueue.py", line 34, in worker_generator
p = Process(target=test_import.import_1, args=(control_queue, runNum))
NameError: global name 'Process' is not defined`
我知道它在哪里,但我从已知的好代码中接受了这个进程调用,所以我不认为这是一个语法错误。有什么帮助吗?
答案 0 :(得分:1)
通常这是由于缺少模块导入。
你有import multiprocessing
吗?
我的代码是:
import multiprocessing
import time
class test_imports:#Test classes remove
def import_1(self, control_queue, thread_number):
print ("Import_1 number %d started") % thread_number
run = True
count = 1
while run:
alive = control_queue.get()
if alive == 't1kill':
print ("Killing thread type 1 number %d") % thread_number
run = False
break
print ("Thread type 1 number %d run count %d") % (thread_number, count)
count = count + 1
def worker_generator(control_queue, threadName, runNum):
if threadName == 'one':
print ("Starting import_1 number %d") % runNum
p = multiprocessing.Process(target=test_imports.import_1, args=(control_queue, runNum))
p.start()
if __name__ == '__main__':
# Establish communication queues
control = multiprocessing.Queue()
runNum = int(raw_input("Enter a number: "))
threadNum = int(raw_input("Enter number of threads: "))
threadName = raw_input("Enter name: ")
thread_Count = 0
print ("Starting threads")
for i in range(threadNum):
worker_generator(control, threadName, i)
thread_Count = thread_Count + 1
time.sleep(runNum)#let threads do their thing
print ("Terminating threads")
for i in range(thread_Count):
control.put("t1kill")
control.put("t2kill")
答案 1 :(得分:1)
你可能做过import multiprocessing
。这很好,因为在你的代码中,你实际上做了:
multiprocessing.Queue()
但是,在执行Process()
时,您忘记将multiprocessing.
放在其前面。
但是,您也可以通过直接导入类来解决此问题:
from multiprocessing import Queue, Process
但是,您必须将multiprocessing.Queue()
更改为Queue()