Question

I am having issues with this multiprocess script I modeled it after the one I found here 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")

This is the error I get when I run it:

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`

I know where it is, but I took that process call from known good code so I don't think it is a syntax error. Any help?

Was it helpful?

Solution

You probably did import multiprocessing. Which is fine, because in your code, you actually did:

multiprocessing.Queue()

But, when doing Process(), you forgot to put multiprocessing. before it.

However, you can also fix this by importing the classes directly:

from multiprocessing import Queue, Process

But then you would have to change multiprocessing.Queue() to just Queue()

OTHER TIPS

Normally this is due to a missing module import.

Did you import multiprocessing?

The code I have is:

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")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top