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


Groups > comp.lang.python > #77227

Re: Thread terminate

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <airween@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'utf-8': 0.07; 'arguments': 0.09; 'caller': 0.09; 'default.': 0.09; 'except:': 0.09; 'exception,': 0.09; 'exception.': 0.09; 'exit': 0.09; 'things,': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'thread': 0.14; '(either': 0.16; 'chris,': 0.16; 'err:': 0.16; 'exit.': 0.16; 'leave.': 0.16; 'log.': 0.16; 'optional': 0.16; 'raised,': 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; 'code,': 0.22; 'example': 0.22; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'looks': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'point': 0.28; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'errors': 0.30; 'needed.': 0.30; 'see,': 0.30; 'code': 0.31; '-0700,': 0.31; 'exceptions': 0.31; 'occurs': 0.31; 'raised': 0.31; 'terminate': 0.31; 'class': 0.32; 'run': 0.32; 'running': 0.33; 'could': 0.34; 'problem': 0.35; 'except': 0.35; 'case,': 0.35; 'no,': 0.35; 'received:84': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'keyword': 0.36; 'thanks': 0.36; 'possible': 0.36; 'similar': 0.36; 'so,': 0.37; 'same.': 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; 'logs': 0.60; 'entire': 0.61; 'from:charset:utf-8': 0.61; 'content-disposition:inline': 0.62; "you'll": 0.62; 'happen': 0.63; 'more': 0.64; 'different': 0.65; 'here': 0.66; 'sample': 0.67; 'between': 0.67; 'statement,': 0.68; 'therefore': 0.72; 'saw': 0.77; 'avoid.': 0.84; 'yours': 0.88; 'received:hu': 0.93; 'directly.': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=bniNz6kizLYQDtKwnjPdGBfivDsXfBAOlwngh8GVEL0=; b=dsGozrUAnBoLPMm3KhTVBoPng4upjVOQh2L293oXEiM3BehfCVfNDHP6OcdBBeWoSS 5HfQ6cHIC9OB/LbuGusZRr5X8EGeh9rMwYNv89vv5IdUPWrZwiEzihkfNs2jI5WsVZzY KlDJt3OuUqbxRS2bHlwE2mjj1zJhKEJ5Es5kfWWcRy2meXfZTUdcIIAJUJyC09x+TwiJ mlwEgQ5SkYGDqfgftnah2xd5Rsh84WqetuKT1YMmM3+QXQsu13FXwO+bXgKqteAtcyn9 SC4Th6ZsXUPOXc/lGRET30FasseXgNJ9kAH1T2qFiCeRMyYlWt4hLgEJhXpKNszKapxo EBzA==
X-Received by 10.180.37.16 with SMTP id u16mr40363352wij.72.1409251143142; Thu, 28 Aug 2014 11:39:03 -0700 (PDT)
Date Thu, 28 Aug 2014 20:39:00 +0200
From Ervin Hegedüs <airween@gmail.com>
To Chris Kaynor <ckaynor@zindagigames.com>
Subject Re: Thread terminate
References <20140827185514.GA18660@arxnet.hu> <CALvWhxsJW+pDKYnf9iZzRHsUg6V9wbNjp=3VqEjGSomez_WQEw@mail.gmail.com> <20140828085211.GC2887@arxnet.hu> <CALvWhxv-WUO-i_XMcPQHxRwS7K5CX6hOCe7msLT5xd7GhV4bdQ@mail.gmail.com>
MIME-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Disposition inline
Content-Transfer-Encoding 8bit
In-Reply-To <CALvWhxv-WUO-i_XMcPQHxRwS7K5CX6hOCe7msLT5xd7GhV4bdQ@mail.gmail.com>
User-Agent Mutt/1.5.21 (2010-09-15)
Cc "python-list@python.org" <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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.13576.1409251151.18130.python-list@python.org> (permalink)
Lines 97
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1409251151 news.xs4all.nl 2863 [2001:888:2000:d::a6]:34932
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:77227

Show key headers only | View raw


Hi Chris,

thanks for you answer,

On Thu, Aug 28, 2014 at 09:23:24AM -0700, Chris Kaynor wrote:
> On Thu, Aug 28, 2014 at 1:52 AM, Ervin Hegedüs <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 off
> > the
> > > error data in a variable (either passed into the thread as the "args" or
> > > "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.

now the code looks like this:

  def run(self):
    try:
      connect_to_db()
    except:
      self._exit(-1, "Connection error", sys.exc_info()[1])
      return True

    try:
      do_another_thing()
    except:
      self._exit(-2, "Another error", sys.exc_info()[1])
      return True

so, the thread's _exit() functions set many things, logs to
exception error, but not terminate the thread. As you can see,
all except line of try block contains a return - that's what I
wanted to avoid.
 
> The threading module will cause it to print any exception that occurs by
> default.

yes, I saw that :),
 
> > 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:

"""I've re-indented your code below:"""

> 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!

Now I don't see the key different between your code, and my
example - looks like there are same. But when I tried to throw an
exception, that showed in TTY. Nevermind, now it works, only the
necessary "return" keyword would be good to leave.

> # If you want to be able to handle the errors in the main thread, you could
> run code like the following:
> 
> #self.err = err
> 
>  # Where you can check and access them on the main thread.
> 
> return # Optional if there is no other code outside the try...except.

I don't see your opinion from this snippet, but I think my
solution is very similar like yours :)


Thanks for the help again,


a.

-- 
I � UTF-8

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Thread terminate Ervin Hegedüs <airween@gmail.com> - 2014-08-28 20:39 +0200

csiph-web