0

我有这个多进程脚本的问题,我在我在这里找到的那​​个之后建模它 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`

我知道它在哪里,但是我从已知的良好代码中获取了该进程调用,所以我认为这不是语法错误。有什么帮助吗?

4

2 回答 2

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")
于 2013-07-28T00:04:57.683 回答
1

你可能做到了import multiprocessing。这很好,因为在您的代码中,您实际上做了:

multiprocessing.Queue()

但是,在做的时候Process(),你忘了把multiprocessing.它放在前面。

但是,您也可以通过直接导入类来解决此问题:

from multiprocessing import Queue, Process

但是,您将不得不更改multiprocessing.Queue()Queue()

于 2013-07-28T00:09:28.733 回答