[PyQt] QTerminate and Python libs?
Giuseppe Corbelli
giuseppe.corbelli at copanitalia.com
Thu Jan 10 08:55:55 GMT 2013
On 09/01/2013 18:03, David Cortesi wrote:
> The doc for QThread.terminate() says its use "is discouraged".
>
> I have a thread that calls urllib2 to read an http page. I am having a problem
> where this thread gets hung somewhere in the urllib2 -- remains to be
> determined whether in urllib2.urlopen() or in a read() on the resulting object.
>
> Anyway, I am thinking of implementing a Kill button so the main thread can
> stop and recreate the worker threadin case of such a hang. But am puzzled on
> several points:
>
> 1. Would .terminate of a QThread that is hung within a python lib, actually
> achieve anything, i.e. break out of the python hang?
Think so. But if the main thread is waiting on the 2nd thread? It may not have
the chance to release mutexes/notify condition variables.
> 2. If it did exit the python lib code, would it be likely to leave python
> itself (or PyQt or Sip) in some bad indeterminate state that would cause
> further problems, e.g. when the thread is recreated and calls back into urllib2?
I think that QThread.terminate() maps directly on QThread::terminate, right?
If so, on unixes the QThread::terminate means pthread_cancel. Have a look at
the man page. A quick look at qt 4.7.0 sources says that cancellation is
enabled by default (src/corelib/thread/qthread_unix.cpp) but nothing calls
pthread_setcanceltype -> cancellation is deferred at cancellation points.
Indeterminate state for sure, but this may mean nothing if there's no data
shared between threads and the library itself has no internal state.
> 3. What about QThread.exit() -- I can't figure out if it is meant for calling
> from within the thread's code, or from outside it? Would it be of use in a
> "kill" situation?
AFAIK can be called anywhere. It just tells the event loop to exit cleanly. If
the event loop does not get called because i.e. the thread is blocked in an IO
operation this call apparently does nothing.
--
Giuseppe Corbelli
WASP Software Engineer, Copan Italia S.p.A
Phone: +390303666318 Fax: +390302659932
E-mail: giuseppe.corbelli at copanitalia.com
More information about the PyQt
mailing list