Re: QThread.terminate in Python 3

Discussion in 'Python' started by Dennis Lee Bieber, Sep 29, 2012.

  1. On Sat, 29 Sep 2012 06:34:00 +0430, Lee Harr <>
    declaimed the following in gmane.comp.python.general:

    >
    > Ok. Point taken.
    >
    > What I hear you saying is that once I use .terminate anything
    > following that is indeterminate. It might work on my system
    > and nowhere else. Even though it was working for me before,
    > it was likely causing trouble elsewhere.
    >

    Even changing a line of code could potentially cause a problem (if
    the terminate action takes place between a different set of Python
    op-codes).

    >
    > So, I need another approach.
    >
    > The problem I am facing is that I want to run arbitrary
    > (user-supplied) code in the other thread and need to be
    > able to stop it at any time.
    >

    The cleanest, that I can visualize (though I can't suggest
    implementation), is probably to run as an independent subprocess (or
    maybe via multiprocess), with some sort of non-blocking (polled?) I/O
    for results. You should be able to use OS-level operations to terminate
    the independent process and ensure the system reclaims all associated
    memory.

    > It uses ctypes to raise an exception in the other thread.
    > For the simple test case, at least, it works (for me).
    >
    > Is this any safer or more reliable than .terminate ?
    >

    Out of my experience; but if the exception is truly in the other
    thread it should be a clean exit -- since the exception detection code
    is part of the Python interpreter logic, it should be saving clean
    states (after all, it it could leave a corrupt state, having exception
    handlers won't help in recovery). Even better would be to wrap the
    thread operation with an exception handler in which you can explicitly
    perform any clean-up (or, at least, log the situation).

    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Sep 29, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pradnyesh Sawant

    problem with qthread and signal/slot

    Pradnyesh Sawant, Apr 14, 2007, in forum: Python
    Replies:
    0
    Views:
    449
    Pradnyesh Sawant
    Apr 14, 2007
  2. daved170

    PyQT Qthread stuck main app

    daved170, Sep 15, 2009, in forum: Python
    Replies:
    0
    Views:
    420
    daved170
    Sep 15, 2009
  3. Lee Harr

    QThread.terminate in Python 3

    Lee Harr, Sep 23, 2012, in forum: Python
    Replies:
    0
    Views:
    197
    Lee Harr
    Sep 23, 2012
  4. Dennis Lee Bieber

    Re: QThread.terminate in Python 3

    Dennis Lee Bieber, Sep 23, 2012, in forum: Python
    Replies:
    0
    Views:
    184
    Dennis Lee Bieber
    Sep 23, 2012
  5. Lee Harr

    Re: QThread.terminate in Python 3

    Lee Harr, Sep 29, 2012, in forum: Python
    Replies:
    0
    Views:
    182
    Lee Harr
    Sep 29, 2012
Loading...

Share This Page