Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Cyborg)
  • No Skin
Collapse
Brand Logo

CIRCLE WITH A DOT

  1. Home
  2. Uncategorized
  3. Maybe I'm missing something, but is there no way from one Python thread, to cause another thread to have an exception raised in it to terminate it?

Maybe I'm missing something, but is there no way from one Python thread, to cause another thread to have an exception raised in it to terminate it?

Scheduled Pinned Locked Moved Uncategorized
python
4 Posts 2 Posters 1 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • encthenet@flyovercountry.socialE This user is from outside of this forum
    encthenet@flyovercountry.socialE This user is from outside of this forum
    encthenet@flyovercountry.social
    wrote last edited by
    #1

    Maybe I'm missing something, but is there no way from one Python thread, to cause another thread to have an exception raised in it to terminate it?

    asyncio has Task Cancellation which is what I want for threads.

    e.g. something similar to pthread_cancel but better?

    I have seen a hack on SO, and I'm not clear that it would interrupt syscalls (e.g. the thread is waiting in select/kqueue and will be signaled so that it can be returned to Python's interpreter to raise the exception).

    #Python

    cazabon@mindly.socialC 1 Reply Last reply
    0
    • encthenet@flyovercountry.socialE encthenet@flyovercountry.social

      Maybe I'm missing something, but is there no way from one Python thread, to cause another thread to have an exception raised in it to terminate it?

      asyncio has Task Cancellation which is what I want for threads.

      e.g. something similar to pthread_cancel but better?

      I have seen a hack on SO, and I'm not clear that it would interrupt syscalls (e.g. the thread is waiting in select/kqueue and will be signaled so that it can be returned to Python's interpreter to raise the exception).

      #Python

      cazabon@mindly.socialC This user is from outside of this forum
      cazabon@mindly.socialC This user is from outside of this forum
      cazabon@mindly.social
      wrote last edited by
      #2

      @encthenet

      Depending on what exactly you're using threads for, the `concurrent.futures` module has a pool model (which you can choose to have run with threads or processes), and that automatically provides exceptions that occur in the worker thread to the parent/main thread when it attempts to retrieve the worker's result.

      encthenet@flyovercountry.socialE 1 Reply Last reply
      1
      0
      • R relay@relay.publicsquare.global shared this topic
      • cazabon@mindly.socialC cazabon@mindly.social

        @encthenet

        Depending on what exactly you're using threads for, the `concurrent.futures` module has a pool model (which you can choose to have run with threads or processes), and that automatically provides exceptions that occur in the worker thread to the parent/main thread when it attempts to retrieve the worker's result.

        encthenet@flyovercountry.socialE This user is from outside of this forum
        encthenet@flyovercountry.socialE This user is from outside of this forum
        encthenet@flyovercountry.social
        wrote last edited by
        #3

        @cazabon concurrent.futures doesn't have the feature I need. It only supports canceling work that hasn't started/dispatched, not work that is currently running.

        I want to inject into a thread an exception to be raised the next time Python executes code. It IS possible, since that happens w/ signal handlers, but signal handlers are only delivered to the main thread.

        cazabon@mindly.socialC 1 Reply Last reply
        0
        • encthenet@flyovercountry.socialE encthenet@flyovercountry.social

          @cazabon concurrent.futures doesn't have the feature I need. It only supports canceling work that hasn't started/dispatched, not work that is currently running.

          I want to inject into a thread an exception to be raised the next time Python executes code. It IS possible, since that happens w/ signal handlers, but signal handlers are only delivered to the main thread.

          cazabon@mindly.socialC This user is from outside of this forum
          cazabon@mindly.socialC This user is from outside of this forum
          cazabon@mindly.social
          wrote last edited by
          #4

          @encthenet

          To make sure I understand what you're describing:

          Parent/main thread starts another thread (call it a worker) to do some task.
          Parent then continues to do <whatever>.
          If worker thread encounters an exception, then:
          parent thread should be interrupted asynchronously in the middle of whatever it happens to be doing.
          That interruption is in the form of the exception raised in the worker thread, raised in the parent wherever it happens to be executing.

          Is that correct?

          If so, I'm afraid I don't know of a way to achieve that in pure Python; it would need a C extension at the least. It could be done (and I've done it) if you can have the parent thread/process occasionally poll for completion/error of the worker(s), and exceptions get propagated at that time, rather than asynchronously.

          You can't really model your behaviour on signal handling because signals are "special" in a number of ways, and in general should be used (a) as little as possible, and (b) to do as little as possible.

          Being able to kill an in-progress job is fairly easy with the process pool option; you can just signal the worker to kill it, even though there's no API for it. Threads are unlikely to behave well if you try it there.

          1 Reply Last reply
          1
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups