Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'arguments': 0.09; 'assigning': 0.09; 'caller': 0.09; 'default.': 0.09; 'exception.': 0.09; 'exit': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'thread': 0.14; '(either': 0.16; 'err:': 0.16; 'exit.': 0.16; 'log.': 0.16; 'mutated': 0.16; 'optional': 0.16; 'raised,': 0.16; 'range(0,': 0.16; 'stdout': 0.16; 'subject:Thread': 0.16; 'thread,': 0.16; 'throw': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'module': 0.19; 'thu,': 0.19; 'aug': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'error': 0.23; 'cc:2**0': 0.24; '>': 0.26; 'handling': 0.26; 'possibly': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'am,': 0.29; 'errors': 0.30; 'needed.': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'exceptions': 0.31; 'occurs': 0.31; 'raised': 0.31; 'class': 0.32; 'run': 0.32; 'received:209.85.212': 0.32; 'running': 0.33; 'could': 0.34; 'problem': 0.35; 'received:209.85': 0.35; 'except': 0.35; 'something': 0.35; 'case,': 0.35; 'no,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'complete.': 0.36; 'object,': 0.36; 'possible': 0.36; 'received:209': 0.37; 'skip:& 10': 0.38; 'problems': 0.38; 'stopped': 0.38; 'handle': 0.38; 'does': 0.39; 'structure': 0.39; 'sure': 0.39; 'unable': 0.39; 'either': 0.39; 'logged': 0.60; 'skip:\xc2 10': 0.60; 'entire': 0.61; "you'll": 0.62; 'happen': 0.63; '8bit%:10': 0.64; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'sample': 0.67; 'statement,': 0.68; 'therefore': 0.72; 'received:209.85.212.177': 0.84; 'received:mail- wi0-f177.google.com': 0.84; 'directly.': 0.95 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=K73ayfH+PPLQBWT5h+EFrzR0QpBiePj2FGy9ISe0714=; b=F6ZGRC9DVbWwBCGfGUnCyTRCvE9bwOe+bUusrLTKy8zw3FeKyfzAwzj5zdVh3/k3ZE rZLU7SM+WGrkcc2xJ/G/lDIrzYDy7EGR5ZK0FyeZWjAAPMVYY1FuVwph9UttyCVzwLG8 sPiDp2f1Qi8yAgBO/3F5H/l/dwvtYCgPZT3MDYzi+hmF8vtu841MrA+lpP0kFudYcfs2 QQXD7QAueHSotUR0rUX9g9tEpWzOVxfapsj6s3//lBX98cIL+kEDGUJl4mH+4ytndmtr /Ttz/UOBqo632NJ1lC6a5GJfDgb51UgD7yZwV4OoPjmTUqUeVS9/IVQpZXesllI4rrsU NcvA== X-Gm-Message-State: ALoCoQlLkKuHVmEc50z8QsyC6SMADW1RMnpNTTYazrAgIg7qbtPuS5DESD+Kc0suFW2SD2dyN4DR X-Received: by 10.180.91.70 with SMTP id cc6mr7518875wib.66.1409243024954; Thu, 28 Aug 2014 09:23:44 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <20140828085211.GC2887@arxnet.hu> References: <20140827185514.GA18660@arxnet.hu> <20140828085211.GC2887@arxnet.hu> From: Chris Kaynor Date: Thu, 28 Aug 2014 09:23:24 -0700 Subject: Re: Thread terminate To: =?UTF-8?Q?Ervin_Heged=C3=BCs?= Content-Type: multipart/alternative; boundary=f46d0438edd7e7aed80501b2f54d Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 224 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409243033 news.xs4all.nl 2885 [2001:888:2000:d::a6]:53024 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77221 --f46d0438edd7e7aed80501b2f54d Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Thu, Aug 28, 2014 at 1:52 AM, Ervin Heged=C3=BCs wro= te: > > > In your case, you may want to just handle the exceptions inside the > > thread's run() function directly. If that is not possible and you reall= y > > need to handle them inside the main thread, you would need to store off > the > > error data in a variable (either passed into the thread as the "args" o= r > > "kwargs" arguments to the MyThread() call, or global or class variables= ) > > then use mt.join() to wait for the thread(s) to exit. > > no, I don't need to handle it outside of thread - the point is > when the exception raised in a running thread, then the exception > be logged (that's no problem), and thread be stopped state, so > the caller function is able to call the join() that thread. In this case, all that needs to happen is for the thread's run function to either throw an exception (as happens in the error case in your sample code) or return. The threading module will cause it to print any exception that occurs by default. > > In the case of handling the problems in the main thread, your main > thread > > code would look something like: > > > > # Start all of the threads up-front. > > > > threads =3D [] > > for q in range(0, 10): > > mt =3D MyThread() > > mt.start() # NOT mt.run() - that does not do any threading! > > threads.append(mt) > > > > # Now wait for them to complete. > > > > for thread in threads: > > thread.join() > > # Check for the status of thread here. Possibly on the thread objec= t, > > which can be mutated in MyThread.run by assigning to members of self. > > > > yes, the pseudo structure of my code is same as your here. But > the now the problem is when an exception raised, that showed at > stdout too, not just the log. > If what you want is to make sure the error is not printed to stderr, you'll just need to make sure the thread's run function does not exit with an exception. The simpliest way to do that would be to wrap the entire thread's run function in a try...catch statement, like so: class Thread(threading.Thread) def run(self): try: # Do your code here. # You can also have more specific error-handling inside here if needed. except Exception as err: # Log your error here - you will be silencing it and therefore unable to see it in the TTY! # If you want to be able to handle the errors in the main thread, you could run code like the following: #self.err =3D err # Where you can check and access them on the main thread. return # Optional if there is no other code outside the try...except. --f46d0438edd7e7aed80501b2f54d Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On T= hu, Aug 28, 2014 at 1:52 AM, Ervin Heged=C3=BCs <airween@gmail.com>= wrote:
> In your case, you may want to just handle the exceptions inside the > thread's run() function directly. If that is not possible and you = really
> need to handle them inside the main thread, you would need to store of= f the
> error data in a variable (either passed into the thread as the "a= rgs" or
> "kwargs" arguments to the MyThread() call, or global or clas= s variables)
> then use mt.join() to wait for the thread(s) to exit.

no, I don't need to handle it outside of thread - the point is when the exception raised in a running thread, then the exception
be logged (that's no problem), and thread be stopped state, so
the caller function is able to call the join() that thread.

In this case, all that needs to happen is for the thread&#= 39;s run function to either throw an exception (as happens in the error cas= e in your sample code) or return.

The threading module will cause it to print any excepti= on that occurs by default.
=C2=A0
> In the case of handling the problems in the main thread, your main thr= ead
> code would look something like:
>
> # Start all of the threads up-front.
>
> threads =3D []
> for q in range(0, 10):
>=C2=A0 =C2=A0 =C2=A0mt =3D MyThread()
>=C2=A0 =C2=A0 =C2=A0mt.start() # NOT mt.run() - that does not do any th= reading!
>=C2=A0 =C2=A0 =C2=A0threads.append(mt)
>
> # Now wait for them to complete.
>
> for thread in threads:
>=C2=A0 =C2=A0 =C2=A0thread.join()
>=C2=A0 =C2=A0 =C2=A0# Check for the status of thread here. Possibly on = the thread object,
> which can be mutated in MyThread.run by assigning to members of self.<= br> >

yes, the pseudo structure of my code is same as your here. But
the now the problem is when an exception raised, that showed at
stdout too, not just the log.

If what y= ou want is to make sure the error is not printed to stderr, you'll just= need to make sure the thread's run function does not exit with an exce= ption. The simpliest way to do that would be to wrap the entire thread'= s run function in a try...catch statement, like so:

class Thread(threading.Thread)
def run(self):
try:
# Do your code here.
# You can also h= ave more specific error-handling inside here if needed.
except Exce= ption as err:
# Log your error here - yo= u will be silencing it and therefore unable to see it in the TTY!
# If you want to= be able to handle the errors in the main thread, you could run code like t= he following:
#self.err =3D err
# Where you can check and access them on th= e main thread.
return # Optional if there= is no other code outside the try...except.
<= /blockquote>

--f46d0438edd7e7aed80501b2f54d--