0

I run a while True loop function in a thread, and I need to terminate the thread from main.

I can't stop the loop with a stop flag because of the function written in another module.

The code looks like this:

import threading
from time import sleep

# This def are in another module
def loop():
    while True:
        print("Stil looping")
        sleep(1)

# Here I define and start the thread
t = threading.Thread(target=loop, args=())
t.start()

# Here is Thinks that happened in my Main
sleep(5)

# Here I try to stop the thread.
t.join()

print("Down")

The output look like this:

Stil looping
Stil looping
Stil looping
Stil looping
Stil looping
Stil looping
Stil looping
Stil looping
Stil looping
Stil looping
Stil looping
...

It doesn't join the thread...

Hope for your help. Thanks.

ruohola
  • 21,987
  • 6
  • 62
  • 97
Ofek Sher
  • 9
  • 1
  • with which condition you want to stop that while loop? – Carlo Zanocco Feb 04 '20 at 15:11
  • Hey @CarloZanocco, I look on changes in an external database, when I see there "disable=true" I want to stop the thread force. – Ofek Sher Feb 04 '20 at 15:18
  • @stovfl Thanks, but it doesn't help me because I can't change the while condition (It in another module and i can't change it...) – Ofek Sher Feb 04 '20 at 15:20
  • I know that is a dirty way but why you can't just simply add into your while loop that condition and break on it? `if disable == true: break` or let your function return something like`return None` – Carlo Zanocco Feb 04 '20 at 15:22
  • The cleanest way wold be to define the variable `disable = False` before the while loop, and use this variable as condition, into the while loop update the value so when it is true you loop terminate. `disable == False \n while not disable: \n #update disable value \n sleep(1)` – Carlo Zanocco Feb 04 '20 at 15:31
  • @CarloZanocco OP cannot change the code of the loop... – ruohola Feb 04 '20 at 15:37
  • Hey, I can’t change the loop because it come from end user script. My script runs few user’s scripts in parallel, using threads. In some case in need to stop one of the scripts in force. This is the reason that I cannot add while condition. Thanks. – Ofek Sher Feb 04 '20 at 15:46
  • @ruohola You are right my bad. He can mark the thread as `daemon` and use the function `_stop()`. Place `t.daemon = True` before the `t.start()` and replace the `t.join()` with `t._stop()`. All in the main program, without changing the while loop.Daemon threads or abuse the `_stop` function can lead to instabilities and oddities as some code will continue executing while the interpreter starts shutting down – Carlo Zanocco Feb 04 '20 at 15:46
  • @CarloZanocco Feel free to try that and see that it won't work. – ruohola Feb 04 '20 at 15:51
  • @ruohola i have alredy tried it and it is actually working. It just depends on your python version, before python 3.3 included the daemon declaretion is not necessary, after that python version you must declare the thread as daemon – Carlo Zanocco Feb 04 '20 at 15:52
  • @CarloZanocco https://repl.it/repls/StickyUnrealisticAbility – ruohola Feb 04 '20 at 16:00
  • @ruohola so my pc is made with magic – Carlo Zanocco Feb 04 '20 at 16:01
  • Thanks @ruohola , But CarloZanocco rights, It doesn't work... – Ofek Sher Feb 04 '20 at 16:09
  • @OfekSher That's what I said... – ruohola Feb 04 '20 at 16:09
  • @ruohola Oh sorry, I got confused. – Ofek Sher Feb 04 '20 at 16:16

0 Answers0