Path: ...!news.mixmin.net!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: ram@zedat.fu-berlin.de (Stefan Ram) Newsgroups: comp.lang.python Subject: Re: How to break while loop based on events raised in a thread (Python 2.7) Date: 27 Nov 2024 14:19:25 GMT Organization: Stefan Ram Lines: 84 Expires: 1 Jan 2026 11:59:58 GMT Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de HCLAHlxtK2le3SDilgy7lwg+yPLLo/Yfq2GJQjo2724WdF Cancel-Lock: sha1:BQftpEt7Yh6/9L9vkSNy2pFOuQg= sha256:+GokBrPq/y2VQiff4Lp3N4FGlzZR3eQ1GrfaFcmozXo= X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed. X-No-Archive: Yes Archive: no X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access. X-No-Html: yes Content-Language: en-US Bytes: 3878 marc nicole wrote or quoted: >Subject: How to break while loop based on events raised in a >thread (Python 2.7) `Threading.Event` is your go-to for thread communication without breaking a sweat. It's a synchronization primitive that lets threads chat with each other using a simple flag mechanism. Think of it as a traffic light for your threads. Here's the lowdown: Creating an event: import threading event = threading.Event() . Checking if the event is set: if event.is_set(): print( "Green light, go!" ) . Setting the event (turning the light green): event.set() . Clearing the event (back to red): event.clear() . Waiting for the event to be set: event.wait() # Will chill here until the event is set . Waiting with a timeout (for the impatient threads): if event.wait(timeout=5): print( "Event was set within 5 seconds" ) else: print( "Timed out, event wasn't set" ) . The cool thing about `threading.Event` is that it's like a bouncer at a club. One thread can be the bouncer (setting or clearing the event), while other threads wait in line (using `wait()`). When the bouncer gives the green light, all waiting threads get to party. Here's a quick example to tie it all together: import threading import time def waiter( event, name ): print( f"{name} is waiting for the event" ) event.wait() print( f"{name} received the event signal!" ) def main(): event = threading.Event() # Create some threads that wait for the event threading.Thread( target=waiter, args=( event, "Thread 1" )).start() threading.Thread( target=waiter, args=( event, "Thread 2" )).start() print( "Main thread: Chilling for 3 seconds before setting the event" ) time.sleep( 3 ) print( "Main thread: Setting the event!" ) event.set() if __name__ == "__main__": main() . This script is like throwing a surprise party. The main thread is setting up, while the other threads are waiting outside. When everything's ready, the main thread yells "Surprise!" (sets the event), and everyone rushes in. Remember, `threading.Event` is your friend when you need simple thread synchronization without the complexity of locks or semaphores. It's perfect for those "wait until I say go" scenarios in your multi-threaded Python fiesta.