2013-07-27 112 views
0

我有這個多進程劇本我仿照它一前一後的問題,我發現這裏 http://broadcast.oreilly.com/2009/04/pymotw-multiprocessing-part-2.html的Python NameError腳本

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

你導入什麼地方? – TerryA

回答

1

你可能沒有import multiprocessing。這是很好的,因爲在你的代碼中,你實際做:

multiprocessing.Queue() 

但是,這樣做Process()時,你忘了在前面加上multiprocessing.

不過,您也可以直接導入類解決這個問題:

from multiprocessing import Queue, Process 

但你必須改變multiprocessing.Queue()只是Queue()

+0

哦哇,一直盯着錯誤的一塊代碼,謝謝@Haidro !!!!!!! –

+0

@KyleSponable不客氣:) – TerryA

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") 
+0

我試圖得到這個錯誤ImportError:沒有模塊命名過程 –

+0

@KyleSponable - 對不起,我的意思是說「import multiprocessing」 – Hayden

+0

是的,我沒有導入多處理,我沒有調用它 –