1

I am new to multiprocessing in Python. I have this below code which is very simple. It does not work though! It just prints the Start and End. It never prints the "Into Run". What am I missing? I am on Windows, Python 2.6

import logging, sys
from multiprocessing import Process

class ZincDataExtract(Process):
    def __init__(self, sources=None):
        Process.__init__(self)
        if sources is None:
            self.src = 'Everything'
        else:
            self.src = sources

    def run(self):
        print "Into Run: " + str(self.src)
        sys.stdout.flush()

def main():

    from datetime import datetime
    cobDate = datetime.strptime('2013-11-29', '%Y-%m-%d').date()

    logging.info("Start: " + datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

    procs = []
    sources='CRE,Mortgage,OGRE,RV2,TDR,Tiger,CIDW'
    p1 = Process(target=ZincDataExtract, args=(sources))
    procs.append(p1)
    p1.start()

    sources='RAM'
    p2 = Process(target=ZincDataExtract, args=(sources))
    procs.append(p2)
    p2.start()

    for p in procs:
        p.join()

    logging.info("End: " + datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
4
  • 1
    Have you tried to reproduce the problem using only a couple of lines? Commented Dec 27, 2013 at 16:12
  • Doesn't seem you ever call Run, from what I can tell. Commented Dec 27, 2013 at 16:15
  • My understanding was that p.start() will call the run() method! Commented Dec 27, 2013 at 16:16
  • Yes, run() is executed on start(). Commented Dec 27, 2013 at 17:17

2 Answers 2

2

As you are extending the multiprocessing.Process class, you should treat ZincDataExtract as if it were that class itself. You are already calling the __init__() method of the super-class (line 5), so you shouldn't need to instantiate two Process classes.

To make the code work, change the lines:

p1 = Process(target=ZincDataExtract, args=(sources))
...
p2 = Process(target=ZincDataExtract, args=(sources))

To:

p1 = ZincDataExtract(sources)
...
p2 = ZincDataExtract(sources)

This will create two instances of ZincDataExtract instead of the two instances of Process, and the code will work!

Sign up to request clarification or add additional context in comments.

Comments

1

Alex O'Neill described what you should do to fix the problem, but did not give an explanation, so here goes...

When you run this line

 p1 = Process(target=ZincDataExtract, args=(sources))

I think you expected it to create a new process of your sub class, and then run as that. But this is not what happens. What happens is that a Process object is created, which uses the function pointed to by target= as the the function to run in that process. In this case, it is a class reference. That means it will run ZincDataExtract.__init__ and then exit. The ZincDataExtract object is created, but is never run, and as the __init__ function finishes, the Process instance created also exits.

That is why you should instead change the above line to

p1 = ZincDataExtract(sources)

since this will create the kind of process you actually expected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.