Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, I'm not sure you understand what <code>join</code> does, if you're using it from a function called <code>terminate</code>. When you call <code>join</code>, all that does is make your parent process wait for the child process to finish. You still need to tell the child process that it needs to finish (e.g., by posting something for it on a queue, or sending it a signal), or you'll end up waiting forever.</p> <p>Second, the fact that you've left the <code>self</code> parameters out of your methods, used an old-style class, created a class attribute where you probably wanted an instance attribute, and started talking about global variables for something that seems perfectly suited for an instance attribute implies that you may have some misunderstandings about classes as well.</p> <p>But the big problem seems to be that you want to map string names to instance attributes (or some other kind of variable). While you <em>can</em> do this, <a href="http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html" rel="nofollow">you almost never want to</a>. Instead of having a separate attribute for each name, just use a single <code>dict</code>, with a value for each name. For example:</p> <pre><code>class worker_manager: # ... def __init__(self): self.children = {} def generate(self, control_queue, threadName, runNum): if threadName == 'one': print ("Starting import_1 number %d") % runNum p = multiprocessing.Process(target=i.import_1, args=(control_queue, runNum)) self.children[threadName] p.start() # ... def terminate(self, threadName): self.children[threadName].join() </code></pre> <p>You can clean this up further by factoring out the common parts of the <code>if</code> blocks. For example, you might want a dispatch table. And, while we're at it, let's use more standard names, 4-character indentation instead of random, a new-style class, etc.:</p> <pre><code>class WorkerManager(object): # ... names = {'one': 'import_1', 'two': 'import_2', 'three': 'import_3'} def __init__(self): self.children = {} def generate(self, control_queue, threadName, runNum): name = WorkerManager.names[threadName] target = i.getattr(name) print ("Starting %s number %d") % (name, runNum) p = multiprocessing.Process(target=target, args=(control_queue, runNum)) self.children[threadName] = p p.start() def terminate(self, threadName): self.children[threadName].join() </code></pre> <p>There's probably a lot more than could be refactored if we could see more of your code, but this should be enough to get you started.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload