<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Aug 28, 2014 at 1:52 AM, Ervin Hegedüs <span dir="ltr"><<a href="mailto:airween@gmail.com" target="_blank">airween@gmail.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="">
> In your case, you may want to just handle the exceptions inside the<br>
> thread's run() function directly. If that is not possible and you really<br>
> need to handle them inside the main thread, you would need to store off the<br>
> error data in a variable (either passed into the thread as the "args" or<br>
> "kwargs" arguments to the MyThread() call, or global or class variables)<br>
> then use mt.join() to wait for the thread(s) to exit.<br>
<br>
</div>no, I don't need to handle it outside of thread - the point is<br>
when the exception raised in a running thread, then the exception<br>
be logged (that's no problem), and thread be stopped state, so<br>
the caller function is able to call the join() that thread.</blockquote><div><br></div><div>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.</div>

<div><br></div><div>The threading module will cause it to print any exception that occurs by default.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="">
> In the case of handling the problems in the main thread, your main thread<br>
> code would look something like:<br>
><br>
> # Start all of the threads up-front.<br>
><br>
> threads = []<br>
> for q in range(0, 10):<br>
>     mt = MyThread()<br>
>     mt.start() # NOT mt.run() - that does not do any threading!<br>
>     threads.append(mt)<br>
><br>
> # Now wait for them to complete.<br>
><br>
> for thread in threads:<br>
>     thread.join()<br>
>     # Check for the status of thread here. Possibly on the thread object,<br>
> which can be mutated in MyThread.run by assigning to members of self.<br>
><br>
<br>
</div>yes, the pseudo structure of my code is same as your here. But<br>
the now the problem is when an exception raised, that showed at<br>
stdout too, not just the log.<br></blockquote><div><br></div><div>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:</div>

<div><br></div><div>class Thread(threading.Thread)</div></div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div>def run(self):</div></div></div></blockquote>

<blockquote style="margin:0 0 0 40px;border:none;padding:0px"><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div>try:</div></div></div></blockquote></blockquote>

<blockquote style="margin:0 0 0 40px;border:none;padding:0px"><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote">

<div># Do your code here.</div></div></div></blockquote></blockquote></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><blockquote style="margin:0 0 0 40px;border:none;padding:0px">

<div class="gmail_extra"><div class="gmail_quote"><div># You can also have more specific error-handling inside here if needed.</div></div></div></blockquote></blockquote></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">

<blockquote style="margin:0 0 0 40px;border:none;padding:0px">except Exception as err:<br></blockquote></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><blockquote style="margin:0 0 0 40px;border:none;padding:0px">

<blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div># Log your error here - you will be silencing it and therefore unable to see it in the TTY!</div></div>
</div>
</blockquote></blockquote></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><blockquote style="margin:0 0 0 40px;border:none;padding:0px">

<div class="gmail_extra"><div class="gmail_quote"><div># If you want to be able to handle the errors in the main thread, you could run code like the following:</div></div></div></blockquote></blockquote></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">

<blockquote style="margin:0 0 0 40px;border:none;padding:0px"><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div>#self.err = err</div></div></div></blockquote>

</blockquote></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra">

<div class="gmail_quote"><div># Where you can check and access them on the main thread.</div></div></div></blockquote></blockquote></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><blockquote style="margin:0 0 0 40px;border:none;padding:0px">

<blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div>return # Optional if there is no other code outside the try...except.</div></div></div></blockquote></blockquote>

</blockquote><br></div>