Question related to multiprocessing.Process

Discussion in 'Python' started by Cen Wang, Jan 19, 2013.

  1. Cen Wang

    Cen Wang Guest

    Hi, when I use multiprocessing.Process in this way:

    from multiprocessing import Process

    class MyProcess(Process):

    def __init__(self):
    Process.__init__(self)

    def run(self):
    print 'x'

    p = MyProcess()
    p.start()

    It just keeps printing 'x' on my command prompt and does not end. But I think MyProcess should print an 'x' and then terminate. I don't why this is happening. I'm using Win7 64 bit, Python 2.7.3. Any idea? Thanks in advance.
    Cen Wang, Jan 19, 2013
    #1
    1. Advertising

  2. On Sat, Jan 19, 2013 at 3:50 PM, Cen Wang <> wrote:
    > Hi, when I use multiprocessing.Process in this way:
    >
    > from multiprocessing import Process
    >
    > class MyProcess(Process):
    >
    > def __init__(self):
    > Process.__init__(self)
    >
    > def run(self):
    > print 'x'
    >
    > p = MyProcess()
    > p.start()
    >
    > It just keeps printing 'x' on my command prompt and does not end. But I think MyProcess should print an 'x' and then terminate. I don't why this is happening. I'm using Win7 64 bit, Python 2.7.3. Any idea? Thanks in advance.


    Multiprocessing on Windows requires that your module be importable. So
    it imports your main module, which instantiates another MyProcess,
    starts it, rinse and repeat. You'll need to protect your main routine
    code:

    if __name__=="__main__":
    p = MyProcess()
    p.start()

    ChrisA
    Chris Angelico, Jan 19, 2013
    #2
    1. Advertising

  3. Cen Wang

    Cen Wang Guest

    Thanks! It now works!
    On Saturday, 19 January 2013 13:05:07 UTC+8, Chris Angelico wrote:
    > On Sat, Jan 19, 2013 at 3:50 PM, Cen Wang <> wrote:
    >
    > > Hi, when I use multiprocessing.Process in this way:

    >
    > >

    >
    > > from multiprocessing import Process

    >
    > >

    >
    > > class MyProcess(Process):

    >
    > >

    >
    > > def __init__(self):

    >
    > > Process.__init__(self)

    >
    > >

    >
    > > def run(self):

    >
    > > print 'x'

    >
    > >

    >
    > > p = MyProcess()

    >
    > > p.start()

    >
    > >

    >
    > > It just keeps printing 'x' on my command prompt and does not end. But I think MyProcess should print an 'x' and then terminate. I don't why this is happening. I'm using Win7 64 bit, Python 2.7.3. Any idea? Thanks in advance.

    >
    >
    >
    > Multiprocessing on Windows requires that your module be importable. So
    >
    > it imports your main module, which instantiates another MyProcess,
    >
    > starts it, rinse and repeat. You'll need to protect your main routine
    >
    > code:
    >
    >
    >
    > if __name__=="__main__":
    >
    > p = MyProcess()
    >
    > p.start()
    >
    >
    >
    > ChrisA
    Cen Wang, Jan 19, 2013
    #3
  4. Cen Wang

    Cen Wang Guest

    Thanks! It now works!
    On Saturday, 19 January 2013 13:05:07 UTC+8, Chris Angelico wrote:
    > On Sat, Jan 19, 2013 at 3:50 PM, Cen Wang <> wrote:
    >
    > > Hi, when I use multiprocessing.Process in this way:

    >
    > >

    >
    > > from multiprocessing import Process

    >
    > >

    >
    > > class MyProcess(Process):

    >
    > >

    >
    > > def __init__(self):

    >
    > > Process.__init__(self)

    >
    > >

    >
    > > def run(self):

    >
    > > print 'x'

    >
    > >

    >
    > > p = MyProcess()

    >
    > > p.start()

    >
    > >

    >
    > > It just keeps printing 'x' on my command prompt and does not end. But I think MyProcess should print an 'x' and then terminate. I don't why this is happening. I'm using Win7 64 bit, Python 2.7.3. Any idea? Thanks in advance.

    >
    >
    >
    > Multiprocessing on Windows requires that your module be importable. So
    >
    > it imports your main module, which instantiates another MyProcess,
    >
    > starts it, rinse and repeat. You'll need to protect your main routine
    >
    > code:
    >
    >
    >
    > if __name__=="__main__":
    >
    > p = MyProcess()
    >
    > p.start()
    >
    >
    >
    > ChrisA
    Cen Wang, Jan 19, 2013
    #4
  5. Cen Wang

    Terry Reedy Guest

    On 1/19/2013 12:05 AM, Chris Angelico wrote:
    > On Sat, Jan 19, 2013 at 3:50 PM, Cen Wang <> wrote:
    >> Hi, when I use multiprocessing.Process in this way:
    >>
    >> from multiprocessing import Process
    >>
    >> class MyProcess(Process):
    >>
    >> def __init__(self):
    >> Process.__init__(self)
    >>
    >> def run(self):
    >> print 'x'
    >>
    >> p = MyProcess()
    >> p.start()
    >>
    >> It just keeps printing 'x' on my command prompt and does not end. But I think MyProcess should print an 'x' and then terminate. I don't why this is happening. I'm using Win7 64 bit, Python 2.7.3. Any idea? Thanks in advance.

    >
    > Multiprocessing on Windows requires that your module be importable. So
    > it imports your main module, which instantiates another MyProcess,
    > starts it, rinse and repeat. You'll need to protect your main routine
    > code:
    >
    > if __name__=="__main__":
    > p = MyProcess()
    > p.start()


    This is documented in
    17.2.3. Programming guidelines
    17.2.3.2. Windows

    --
    Terry Jan Reedy
    Terry Reedy, Jan 19, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maxwell Hammer
    Replies:
    7
    Views:
    624
    Peter Hansen
    Jun 18, 2005
  2. James Mills

    subclassing multiprocessing.Process

    James Mills, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    776
    James Mills
    Jan 8, 2009
  3. Replies:
    1
    Views:
    325
    Jesse Noller
    Apr 30, 2009
  4. Thomas Robitaille

    multiprocessing and process run time

    Thomas Robitaille, Jun 19, 2009, in forum: Python
    Replies:
    2
    Views:
    271
    Piet van Oostrum
    Jun 19, 2009
  5. Scott Pigman
    Replies:
    6
    Views:
    317
    Scott Pigman
    Oct 4, 2010
Loading...

Share This Page