0

I know that I can block the current thread with time.sleep(0xFFFFFFFFF) but is there other way?

I know that this may be seem silly, but there are use cases.

For example this could be used inside a try except to catch KeyboardInterrupt exception.

See this: https://stackoverflow.com/a/69744286/1951448

Or if there are daemonic threads running and there is nothing more to do, but don't want the threads be killed, then the main thread has to be suspended.

To clarify, I dont want to kill the thread, I want to suspend it.

nadapez
  • 2,603
  • 2
  • 20
  • 26
  • Does this answer your question? [Is there any way to kill a Thread?](https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread) – Tomerikoo Oct 28 '21 at 16:44
  • 5
    This seems like a really strange thing to do, which leads me to believe this is an XY question. _Why_ do you want a thread to sleep forever? – Tim Wescott Oct 28 '21 at 16:45
  • See "[What is the XY problem?](https://meta.stackexchange.com/a/66378/914217)." – Solomon Slow Oct 28 '21 at 16:49
  • I updated the question to explain why – nadapez Oct 28 '21 at 16:51
  • Why not `while True: pass` – Mateus Terra Oct 28 '21 at 16:53
  • Install a signal handler to react to `SIGINT` instead of waiting for `KeyboardInterrupt` to be raised. – chepner Oct 28 '21 at 16:53
  • 2
    Your update doesn't explain anything. We already knew that you wanted the thread to sleep forever instead of allowing it to be killed. The question is, why do you not want the thread to die? How do you think it would benefit your program to keep a thread that won't do anything, ever again? – Solomon Slow Oct 28 '21 at 16:53
  • 2
    @MateusTerra, that is not what "sleep" means. `while True: sleep(6000)` would be better. – Solomon Slow Oct 28 '21 at 16:55
  • I explained in the question why I want to do that. : " For example this could be used inside a try except to catch KeyboardInterrupt exception. Or if there are daemonic threads running and there is nothing more to do, but don't want the threads be killed, then the main thread has to be suspended. " – nadapez Oct 28 '21 at 17:01
  • And at any case I dont have to give an explanation of why I want to do whatever I want. Is enough that the question is clear. A question can have no practical implication, but can lead to a deep understanding of how things work. Einstein ask himself what would happen if he ride a light ray. Is that a silly question? – nadapez Oct 28 '21 at 17:03
  • With respect with the xy problem I actually come up with the question to solve a question of this forum. I suggested to use a dummy `input()` or `sleep(999999)` to solve the problem https://stackoverflow.com/a/69744286/1951448 . It worked. But I wondered if it could be replaced by something else. – nadapez Oct 28 '21 at 17:11
  • 1
    I disagree with this question being closed; it’s clear and unambiguous and there is a well-defined answer to be given for it. The fact that some people question the motivation behind the question isn’t relevant to the validity of the question itself. – Jeremy Friesner Oct 28 '21 at 17:35
  • @SolomonSlow, I do undestand that it is not sleep, however, there is no need for a sleep at all. All the OP wants is to keep the thread alive. – Mateus Terra Oct 29 '21 at 17:12
  • 2
    @MateusTerra spinning a CPU forever is a fantastically inefficient way to keep a thread alive... laptop owners will not be happy with you :) – Jeremy Friesner Oct 29 '21 at 23:46
  • A use case I have is that I turn on some LED behavior that runs forever in some thread or until the application stops it. – Brian Reinhold May 20 '22 at 15:18

1 Answers1

1

It's unusual to want to block a thread indefinitely, so AFAIK there isn't an API designed specifically for that use case.

The simplest way to achieve that goal would be to call time.sleep() with a large value, as you suggested; wrap it in a while True: loop so that even if the specified time-period expires, your thread will wake up and then go immediately back to sleep again.

OTOH if for some reason you want to guarantee that your thread never wakes up at all, no matter how much time passes, you could have it call recv() on a socket that you are certain will never actually receive any data, e.g.:

import socket

print("About to sleep forever")
[sockA, sockB] = socket.socketpair()
junk = sockA.recv(1)  # will never return since sockA will never receive any data

print("This should never get printed")
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234