Interrupt python thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BlueBird

    Interrupt python thread

    Hi,

    I have a program with a master thread and several slave threads.

    Whenever an exception occurs, in the master thread or in one of the
    slave threads, I would like to interrupt all the threads and the main
    program. Threading API does not seem to provide a way to stop a
    thread, is there anyway to achieve that ?

    The closest thing I found is thread.interrup t_main() but it's far from
    perfect :
    - it only allow to interrupt the main thread
    - if the main thread is sleeping, it does not interrupt it (at least
    on windows)

    cheers,

    Philippe
  • Fredrik Lundh

    #2
    Re: Interrupt python thread

    James Matthews wrote:
    Group all the threads in a list and call the stop method on all of them.
    what stop method?

    </F>

    Comment

    • Fredrik Lundh

      #3
      Re: Interrupt python thread

      James Matthews wrote:
      def __stop(self):
      self.__block.ac quire()
      self.__stopped = True
      self.__block.no tifyAll()
      self.__block.re lease()
      have you tried using that method? what happened? looking at the code,
      what do you think it does?

      </F>

      Comment

      • janislaw

        #4
        Re: Interrupt python thread

        On 24 Sie, 10:48, BlueBird <p...@freehacke rs.orgwrote:
        >
        Whenever an exception occurs, in the master thread or in one of the
        slave threads, I would like to interrupt all the threads and the main
        program. Threading API does not seem to provide a way to stop a
        thread, is there anyway to achieve that ?
        Note that killing, stopping, suspending and resuming Your thread at
        arbitrary time leads to unpredictable results. For this reason i.e.
        Java deprecated all such functions, and python didn't introduce them
        at all. It makes sense as it leads to better-designed-code.

        Regards
        JW


        Comment

        • BlueBird

          #5
          Re: Interrupt python thread

          On Aug 24, 8:35 pm, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
          On Sun, 24 Aug 2008 01:48:46 -0700 (PDT), BlueBird
          <p...@freehacke rs.orgdeclaimed the following in comp.lang.pytho n:
          >
          >
          >
          Whenever an exception occurs, in the master thread or in one of the
          slave threads, I would like to interrupt all the threads and the main
          program. Threading API does not seem to provide a way to stop a
          thread, is there anyway to achieve that ?
          >
                  The only safe way to "abort" a thread is by having it exit on its
          own. This means one needs a means of setting an attribute that each
          thread periodically checks within a while loop.
          >
          Unfortunately, this does not map very well with my program. Each of my
          threads are calling foreign code (still written in python though),
          which might be busy for 1 to 10 minutes with its own job.

          I wanted something to easily interrupt every thread to prevent my
          program to stall for 10 minutes if I want to stop it (getting tired of
          killing python all the time).

          Philippe

          Comment

          • Gabriel Genellina

            #6
            Re: Interrupt python thread

            En Mon, 25 Aug 2008 05:00:07 -0300, BlueBird <phil@freehacke rs.org>
            escribi�:
            On Aug 24, 8:35 pm, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
            >        The only safe way to "abort" a thread is by having it exit on
            >its
            >own. This means one needs a means of setting an attribute that each
            >thread periodically checks within a while loop.
            >>
            >
            Unfortunately, this does not map very well with my program. Each of my
            threads are calling foreign code (still written in python though),
            which might be busy for 1 to 10 minutes with its own job.
            >
            I wanted something to easily interrupt every thread to prevent my
            program to stall for 10 minutes if I want to stop it (getting tired of
            killing python all the time).
            If the foreign Python code is running on your own process, can't you make
            it check a flag periodically?
            If it runs as another process, killing it is a lot safer than killing a
            thread.

            --
            Gabriel Genellina

            Comment

            • Dieter Maurer

              #7
              Re: Interrupt python thread

              En Mon, 25 Aug 2008 05:00:07 -0300, BlueBird <phil@freehacke rs.org>
              escribi¡Z:
              Unfortunately, this does not map very well with my program. Each of my
              threads are calling foreign code (still written in python though),
              which might be busy for 1 to 10 minutes with its own job.
              >
              I wanted something to easily interrupt every thread to prevent my
              program to stall for 10 minutes if I want to stop it (getting tired of
              killing python all the time).
              At the C level, Python has function to send an exception to a thread.
              The threads will see the exception only when it executes Python code
              (i.e. not when it is waiting or running in external (e.g. "C") code).

              You may use (e.g.) "PyRex" to make a Python wrapper available
              to your Python code.

              Dieter

              Comment

              Working...