Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quitting multiple threads
#1
Hi there.

I'm looking into thread.interrupt_main() as a way to exit a thread and close an app irrespective of other threads running.

But, if I've started my app off with:
threads=[threading.Thread(target=func) for func in [ThreadOne, ThreadTwo$
[th.start() for th in threads]
[th.join() for th in threads]

Where does the keyboard handling code go? I don't have a loop anywhere in the main thread anymore?

Many thanks.
Reply
#2
Did the last thread not help?

https://python-forum.io/Thread-starting-...8#pid60378
Reply
#3
Use threading.Event()
Share this object across all threads you want to close. Instead of using while True, use while not event.is_set():

In the manager thread you set the event: event.set()
Then all threads which are still executing, leaves the loop.

Other possible way are signals. I never used them for threads...
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
(Oct-16-2018, 09:14 PM)MuntyScruntfundle Wrote:
  [th.start() for th in threads]
  [th.join() for th in threads]

Please, don't replace for loops with parasitic list comprehensions with side effects.

This code just starts threads
for th in threads:
    th.start()
This code starts threads - and builds a list of None values which is immediately discarded.
  [th.start() for th in threads]
While many adopted this style believing it to be Pythonic - it is essentially un-Pythonic, since you implicitly produces parasitic side effect.

Let me demonstrate on a simple function that does nothing
Output:
In [7]: def demo(): ...: pass ...: ...: In [8]: %%timeit ...: for _ in range(10000): ...: demo() ...: 698 µs ± 5.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) In [9]: %timeit [demo() for _ in range(10000)] 838 µs ± 12.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
As you can clearly see, using comprehension instead of a loop (less sexy Naughty , I know) costs you about 12% overhead Hand . Where did the resource go? It was wasted managing the redundant temporary list Doh .

Do you comprehend Cool ?
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to separate a loop across multiple threads stylingpat 0 1,651 May-05-2021, 05:21 PM
Last Post: stylingpat
  Get average of multiple threads Contra_Boy 1 14,026 May-05-2020, 04:51 PM
Last Post: deanhystad
  stop multiple threads jeuvrey 5 3,328 Nov-15-2018, 01:34 PM
Last Post: jeuvrey
  How to run multiple threads properly cyberion1985 6 5,776 Dec-29-2017, 09:45 AM
Last Post: cyberion1985

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020