Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44289 > unrolled thread

Finding the source of an exception in a python multiprocessing program

Started byWilliam Ray Wing <wrw@mac.com>
First post2013-04-24 15:25 -0400
Last post2013-04-24 20:11 -0400
Articles 7 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Finding the source of an exception in a python multiprocessing program William Ray Wing <wrw@mac.com> - 2013-04-24 15:25 -0400
    Re: Finding the source of an exception in a python multiprocessing program Neil Cerutti <neilc@norwich.edu> - 2013-04-24 20:31 +0000
      Re: Finding the source of an exception in a python multiprocessing program William Ray Wing <wrw@mac.com> - 2013-04-24 17:09 -0400
        Re: Finding the source of an exception in a python multiprocessing program Neil Cerutti <neilc@norwich.edu> - 2013-04-25 12:37 +0000
      Re: Finding the source of an exception in a python multiprocessing program Dave Angel <davea@davea.name> - 2013-04-24 19:26 -0400
      Re: Finding the source of an exception in a python multiprocessing program Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-25 01:00 +0100
      Re: Finding the source of an exception in a python multiprocessing program Dave Angel <davea@davea.name> - 2013-04-24 20:11 -0400

#44289 — Finding the source of an exception in a python multiprocessing program

FromWilliam Ray Wing <wrw@mac.com>
Date2013-04-24 15:25 -0400
SubjectFinding the source of an exception in a python multiprocessing program
Message-ID<mailman.1032.1366835159.3114.python-list@python.org>
I run a bit of python code that monitors my connection to the greater Internet.  It checks connectivity to the requested target IP addresses, logging both successes and failures, once every 15 seconds.  I see failures quite regularly, predictably on Sunday nights after midnight when various networks are undergoing maintenance.  I'm trying to use python's multiprocessing library to run multiple copies in parallel to check connectivity to different parts of the country (they in no way interact with each other).

On rare occasions (maybe once every couple of months) I get the following exception and traceback:

Traceback (most recent call last):
  File "./CM_Harness.py", line 12, in <module>
    Foo = pool.map(monitor, targets)    # and hands off two targets
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 227, in map
    return self.map_async(func, iterable, chunksize).get()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 528, in get
    raise self._value
IndexError: list index out of range

The code where the traceback occurs is:

#!/usr/bin/env python

""" Harness to call multiple parallel copies
    of the basic monitor program
"""    

from multiprocessing import Pool
from Connection_Monitor import monitor

targets = ["8.8.8.8", "www.ncsa.edu"]
pool = Pool(processes=2)            # start 2 worker processes
Foo = pool.map(monitor, targets)    # and hands off two targets


Line 12, in my code is simply the line that launches the underlying monitor code.  I'm assuming that the real error is occurring in the monitor program that is being launched, but I'm at a loss as to what to do to get a better handle on what's going wrong. Since, as I said, I see failures quite regularly, typically on Sunday nights after midnight when various networks are undergoing maintenance, I don't _think_ the exception is being triggered by that sort of failure.

When I look at the pool module, the error is occurring in get(self, timeout=None) on the line after the final else:

    def get(self, timeout=None):
        self.wait(timeout)
        if not self._ready:
            raise TimeoutError
        if self._success:
            return self._value
        else:
            raise self._value


Python v 2.7.3, from Python.org, running on Mac OS-X 10.8.3

Thanks for any suggestions,
Bill

[toc] | [next] | [standalone]


#44291

FromNeil Cerutti <neilc@norwich.edu>
Date2013-04-24 20:31 +0000
Message-ID<atqtpaFa3niU2@mid.individual.net>
In reply to#44289
On 2013-04-24, William Ray Wing <wrw@mac.com> wrote:
> When I look at the pool module, the error is occurring in
> get(self, timeout=None) on the line after the final else:
>
>     def get(self, timeout=None):
>         self.wait(timeout)
>         if not self._ready:
>             raise TimeoutError
>         if self._success:
>             return self._value
>         else:
>             raise self._value

The code that's failing is in self.wait. Somewhere in there you
must be masking an exception and storing it in self._value
instead of letting it propogate and crash your program. This is
hiding the actual context.

-- 
Neil Cerutti

[toc] | [prev] | [next] | [standalone]


#44295

FromWilliam Ray Wing <wrw@mac.com>
Date2013-04-24 17:09 -0400
Message-ID<mailman.1034.1366841384.3114.python-list@python.org>
In reply to#44291
On Apr 24, 2013, at 4:31 PM, Neil Cerutti <neilc@norwich.edu> wrote:

> On 2013-04-24, William Ray Wing <wrw@mac.com> wrote:
>> When I look at the pool module, the error is occurring in
>> get(self, timeout=None) on the line after the final else:
>> 
>>    def get(self, timeout=None):
>>        self.wait(timeout)
>>        if not self._ready:
>>            raise TimeoutError
>>        if self._success:
>>            return self._value
>>        else:
>>            raise self._value
> 
> The code that's failing is in self.wait. Somewhere in there you
> must be masking an exception and storing it in self._value
> instead of letting it propogate and crash your program. This is
> hiding the actual context.
> 
> -- 
> Neil Cerutti
> -- 
> http://mail.python.org/mailman/listinfo/python-list

I'm sorry, I'm not following you.  The "get" routine (and thus self.wait) is part of the "pool" module in the Python multiprocessing library.
None of my code has a class or function named "get".

-Bill

[toc] | [prev] | [next] | [standalone]


#44333

FromNeil Cerutti <neilc@norwich.edu>
Date2013-04-25 12:37 +0000
Message-ID<atsmbfFldo9U1@mid.individual.net>
In reply to#44295
On 2013-04-24, William Ray Wing <wrw@mac.com> wrote:
> On Apr 24, 2013, at 4:31 PM, Neil Cerutti <neilc@norwich.edu> wrote:
>
>> On 2013-04-24, William Ray Wing <wrw@mac.com> wrote:
>>> When I look at the pool module, the error is occurring in
>>> get(self, timeout=None) on the line after the final else:
>>> 
>>>    def get(self, timeout=None):
>>>        self.wait(timeout)
>>>        if not self._ready:
>>>            raise TimeoutError
>>>        if self._success:
>>>            return self._value
>>>        else:
>>>            raise self._value
>> 
>> The code that's failing is in self.wait. Somewhere in there you
>> must be masking an exception and storing it in self._value
>> instead of letting it propogate and crash your program. This is
>> hiding the actual context.
>
> I'm sorry, I'm not following you.  The "get" routine (and thus
> self.wait) is part of the "pool" module in the Python
> multiprocessing library. None of my code has a class or
> function named "get".

Oops! I failed to notice it was part of the pool module and not
your own code. 

-- 
Neil Cerutti

[toc] | [prev] | [next] | [standalone]


#44300

FromDave Angel <davea@davea.name>
Date2013-04-24 19:26 -0400
Message-ID<mailman.1037.1366846014.3114.python-list@python.org>
In reply to#44291
On 04/24/2013 05:09 PM, William Ray Wing wrote:
> On Apr 24, 2013, at 4:31 PM, Neil Cerutti <neilc@norwich.edu> wrote:
>
>> On 2013-04-24, William Ray Wing <wrw@mac.com> wrote:
>>> When I look at the pool module, the error is occurring in
>>> get(self, timeout=None) on the line after the final else:
>>>
>>>     def get(self, timeout=None):
>>>         self.wait(timeout)
>>>         if not self._ready:
>>>             raise TimeoutError
>>>         if self._success:
>>>             return self._value
>>>         else:
>>>             raise self._value
>>
>> The code that's failing is in self.wait. Somewhere in there you
>> must be masking an exception and storing it in self._value
>> instead of letting it propogate and crash your program. This is
>> hiding the actual context.
>>
>> --
>> Neil Cerutti
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
> I'm sorry, I'm not following you.  The "get" routine (and thus self.wait) is part of the "pool" module in the Python multiprocessing library.
> None of my code has a class or function named "get".
>
> -Bill
>

My question is why bother with multithreading?  Why not just do these as 
separate processes?  You said "they in no way interact with each other" 
and that's a clear clue that separate processes would be cleaner.

Without knowing anything about those libraries, I'd guess that somewhere 
they do store state in a global attribute or equivalent, and when that 
is accessed by both threads, it can crash.

Separate processes will find it much more difficult to interact, which 
is a good thing most of the time.  Further, they seem to be scheduled 
more efficiently because of the GIL, though that may not make that much 
difference when you're time-limited by network data.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#44303

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-04-25 01:00 +0100
Message-ID<mailman.1039.1366848077.3114.python-list@python.org>
In reply to#44291
On 25 April 2013 00:26, Dave Angel <davea@davea.name> wrote:
> On 04/24/2013 05:09 PM, William Ray Wing wrote:
>>
>> On Apr 24, 2013, at 4:31 PM, Neil Cerutti <neilc@norwich.edu> wrote:
>>
>>> On 2013-04-24, William Ray Wing <wrw@mac.com> wrote:
>>>>
>>>> When I look at the pool module, the error is occurring in
>>>> get(self, timeout=None) on the line after the final else:
>>>>
>>>>     def get(self, timeout=None):
>>>>         self.wait(timeout)
>>>>         if not self._ready:
>>>>             raise TimeoutError
>>>>         if self._success:
>>>>             return self._value
>>>>         else:
>>>>             raise self._value
>>>
>>>
>>> The code that's failing is in self.wait. Somewhere in there you
>>> must be masking an exception and storing it in self._value
>>> instead of letting it propogate and crash your program. This is
>>> hiding the actual context.
>>>
>>> --
>>> Neil Cerutti
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
>> I'm sorry, I'm not following you.  The "get" routine (and thus self.wait)
>> is part of the "pool" module in the Python multiprocessing library.
>> None of my code has a class or function named "get".
>>
>> -Bill
>>
>
> My question is why bother with multithreading?  Why not just do these as
> separate processes?  You said "they in no way interact with each other" and
> that's a clear clue that separate processes would be cleaner.

It's using multiprocessing rather than threads: they are separate processes.

>
> Without knowing anything about those libraries, I'd guess that somewhere
> they do store state in a global attribute or equivalent, and when that is
> accessed by both threads, it can crash.

It's state that is passed to it by the subprocess and should only be
accessed by the top-level process after the subprocess completes (I
think!).

>
> Separate processes will find it much more difficult to interact, which is a
> good thing most of the time.  Further, they seem to be scheduled more
> efficiently because of the GIL, though that may not make that much
> difference when you're time-limited by network data.

They are separate processes and do not share the GIL (unless I'm very
much mistaken). Also I think the underlying program is limited by the
call to sleep for 15 seconds.


Oscar

[toc] | [prev] | [next] | [standalone]


#44305

FromDave Angel <davea@davea.name>
Date2013-04-24 20:11 -0400
Message-ID<mailman.1041.1366848732.3114.python-list@python.org>
In reply to#44291
On 04/24/2013 08:00 PM, Oscar Benjamin wrote:
> On 25 April 2013 00:26, Dave Angel <davea@davea.name> wrote:
>> On 04/24/2013 05:09 PM, William Ray Wing wrote:
>>>
>>>
>>>    <SNIP>
>>>
>>
>> My question is why bother with multithreading?  Why not just do these as
>> separate processes?  You said "they in no way interact with each other" and
>> that's a clear clue that separate processes would be cleaner.
>
> It's using multiprocessing rather than threads: they are separate processes.
>

You're right;  I was completely off base.  brain-freeze.

>>
>>     <SNIP>
>
> It's state that is passed to it by the subprocess and should only be
> accessed by the top-level process after the subprocess completes (I
> think!).
>
>>
>> Separate processes will find it much more difficult to interact, which is a
>> good thing most of the time.  Further, they seem to be scheduled more
>> efficiently because of the GIL, though that may not make that much
>> difference when you're time-limited by network data.
>
> They are separate processes and do not share the GIL (unless I'm very
> much mistaken).

No, you're not mistaken.  Somehow I interpreted the original as saying 
multi-thread, and everything else was wrong as a result.  Now it sounds 
like a bug in, or misuse of, the Pool class.



-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web