Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #60: system has been recalled


comp / comp.lang.python / Re: How to break while loop based on events raised in a thread (Python 2.7)

SubjectAuthor
* How to break while loop based on events raised in a thread (Python 2.7)marc nicole
`- Re: How to break while loop based on events raised in a thread (Python 2.7)Stefan Ram

1
Subject: How to break while loop based on events raised in a thread (Python 2.7)
From: marc nicole
Newsgroups: comp.lang.python
Date: Wed, 27 Nov 2024 12:28 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: mk1853387@gmail.com (marc nicole)
Newsgroups: comp.lang.python
Subject: How to break while loop based on events raised in a thread (Python
2.7)
Date: Wed, 27 Nov 2024 13:28:06 +0100
Lines: 55
Message-ID: <mailman.1.1732710433.2965.python-list@python.org>
References: <CAGJtH9SuFdPOF80BW2x+obM7-CbaqRuHOnraSmxmD+-yLqOOPw@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de hAZHCmMrFs2cH8RBzgGrYwCSrLNFiCaA/SYSvBbHz/7g==
Cancel-Lock: sha1:H0E9iJBsZJZiEXW4DJCYdTOSdGE= sha256:Uk01oUg3sfgalJB7CurNLbYxKmIWR0TVU0Zc4E4j08A=
Return-Path: <mk1853387@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=UiWU0HXv;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.028
X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'loop': 0.07; 'breaks':
0.09; 'elif': 0.09; 'else:': 0.09; 'subject:Python': 0.12; '&gt;':
0.14; 'accordingly': 0.16; 'condition.': 0.16; 'instance': 0.16;
'outer': 0.16; 'secs': 0.16; 'skip:s 230': 0.16; 'subject:2.7':
0.16; 'subject:loop': 0.16; 'subject:thread': 0.16;
'subject:while': 0.16; 'problem': 0.16; 'to:addr:python-list':
0.20; 'instructions': 0.22; 'to:no real name:2**1': 0.22; 'code':
0.23; 'cannot': 0.25; 'perform': 0.26; 'sfxlen:2': 0.31;
'subject:How': 0.31; 'takes': 0.31; 'message-id:@mail.gmail.com':
0.31; 'program': 0.32; 'checks': 0.32; 'execution': 0.32;
'performed': 0.32; 'but': 0.32; 'there': 0.33;
'received:google.com': 0.34; 'requires': 0.34;
'from:addr:gmail.com': 0.34; 'following': 0.35; 'using': 0.37;
'class': 0.37; 'break': 0.39; 'skip:u 20': 0.39; 'seconds': 0.40;
'want': 0.40; 'event': 0.40; 'method': 0.61; 'true': 0.63; 'skip:b
10': 0.63; 'pass': 0.64; 'upon': 0.65; 'skip:t 20': 0.65;
'8bit%:21': 0.68; 'further': 0.69; 'below': 0.69; 'raised': 0.70;
'<span': 0.76; '2-3': 0.84; 'subject:based': 0.84; 'instant': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1732710430; x=1733315230; darn=python.org;
h=to:subject:message-id:date:from:mime-version:from:to:cc:subject
:date:message-id:reply-to;
bh=dFzoplmYisymksn1w+ELUXiotLX42gOpUVZ7tJCmAZk=;
b=UiWU0HXvGNyfmhQWJnqnFasVDnpaSoppZEBSfs9GyONo/rVNrfQci9MwdinZ9gVb9U
DgIF+1bVfLcA34ujqctePYoFJM/T8KTCmWuF6rsKeFOzbWNSYLu/HrJXAUxEz5ywgDIi
NAPej13LWENOyhUNjGv5Qi0hnQ9DwdDhZ62bn+5c6b3PjTzAGWBzALaLhT5MD7LpFNOA
OrUE7Z6gn04WAqKpWW1UOcq0V3fdJ8e7sxAdiy1Akk3GQQGNCpHGH/Vbn5YZGSAaIIPm
bEAc317TgJD1XZnJc9da2e1BFUwDo5Gg/dJU4beaHW4n3POdntJ9BbXeSGr9FdHFzN4u
8TiA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1732710430; x=1733315230;
h=to:subject:message-id:date:from:mime-version:x-gm-message-state
:from:to:cc:subject:date:message-id:reply-to;
bh=dFzoplmYisymksn1w+ELUXiotLX42gOpUVZ7tJCmAZk=;
b=TdPQNQYPgsi7uRwejjqTSR45Sgg2x6Cs+uPbwy2g0CNhiyupgQSAyhykc3L9qLiGVp
kBTbVrEQlamRYVLwc91MctkoqjQaYP54N3Ntwc9qqpsffXrZNcjkmIk5UIAtcLVz6y0r
PKdpe8ia9yhQ7YUiu7eDfPM3oLz+V0wepmhNFpI67c6wbqur8BQDOVFaMYbbWcK/jCTg
TlCUthV4JzWIrflDuqbpLndpfq7+2I30YA0IVAeW4GAYYPM3vlqTNnBOZ9e88iJri1ZQ
e3oig7RmzPwbAS+D6NZgpCIG1Kvd7ns2wQ01G4tj9ie/lMW08FsX9R0KoVgM0ivOmFY/
wMGQ==
X-Forwarded-Encrypted: i=1;
AJvYcCVZb9RHRWXsBmo1ZZTLpREDECEbyjh2hFi0i0qi3fX4trJfCuvBb6OYTG7E7MAfk+onvXJA3W6w0/otCw==@python.org
X-Gm-Message-State: AOJu0YzLDHkmKPtxuQzVn6iVlAs2iUD0Fi0nMv8S5Kx10w6HULGn3eDb
hoLx7CUDkHXziTNBkeuYUy45Ohh8lL8UTdMUfRgC7LWygtJUNZrNotcO4i+QlyQoHOD7Diqu2At
v7GsEml2Yj3j32KioTnzlRo0dWDY40lfW
X-Gm-Gg: ASbGnct4UHfphzSbbksROg5Zt4Xqo2AvkHOtiOwPn+f9eK5JXcXYwW47vJ3WUuGGqWQ
c1erhvUTYD0kI1KnqwvGyi2D+tdKX/0Rp
X-Google-Smtp-Source: AGHT+IETGX6cHk8swYmiJzpxOcMhzTi/bp2suAA0k9z3qo9BI/FqPD9s/sXPUTYeZfflSDiI3J44yIUYk8kMXW6dl8U=
X-Received: by 2002:a05:690c:6113:b0:6e5:aaf7:68d0 with SMTP id
00721157ae682-6ef3721490fmr30485517b3.18.1732710428628; Wed, 27 Nov 2024
04:27:08 -0800 (PST)
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAGJtH9SuFdPOF80BW2x+obM7-CbaqRuHOnraSmxmD+-yLqOOPw@mail.gmail.com>
View all headers

I am using the below class1 code to detect some event through the method

check_for_the_event()
# Some class1def check_for_the_event(self):
thread1 = threading.Timer(2, self.check_for_the_event)
thread1.start()# some processingif event is detected:
self.event_ok = Trueelse:
self.event_ok = False

then I pass an instance of that class to the below class to know when the
event is on or off and act upon its value accordingly using the following
below code:

# execution of other part of the program (where
self.another_class_instance.event_ok = False and
self.some_other_condition is true)
current_time = current_time_some_cond = current_max_time
= time.time()
while not self.another_class_instance.event_ok and
self.some_other_condition:
while self.some_other_condition and not
self.another_class_instance.event_ok:#self.some_other_condition takes
up to 10 secs to be checked (minimum 5 secs)
if time.time() > current_time + 5:
current_time = time.time()
# some processing
else:
while not self.another_class_instance.event_ok:
#processing
if time.time() > current_max_time + 60 * 2 and
not self.another_class_instance.event_ok:
#some other processing
if time.time() > current_time_some_cond + 10
and not self.cond1 and not self.another_class_instance.event_ok:
# some processing that takes 2-3 seconds
self.cond1 = True
current_time_some_cond = time.time()
elif self.cond1 and time.time() >
current_time_some_cond + 10 and not
self.another_class_instance.event_ok:
current_time_some_cond = time.time()
#some other processing that takes 2-3 seconds
else:
pass
else:
pass

The problem is the real time execution of the program (in class2) requires
an instant check to be performed and that cannot wait until the current
loop instructions finish, and I want is to break out of the outer while
loop if the event is on without further checking any other condition.

for now I perform multiple checks at each if or while statement, but is
there a IO async based method that breaks out of the loop when the event is
raised in the thread?

Subject: Re: How to break while loop based on events raised in a thread (Python 2.7)
From: Stefan Ram
Newsgroups: comp.lang.python
Organization: Stefan Ram
Date: Wed, 27 Nov 2024 14:19 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!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: <event-20241127151444@ram.dialup.fu-berlin.de>
References: <mailman.1.1732710433.2965.python-list@python.org>
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
View all headers

marc nicole <mk1853387@gmail.com> 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.

1

rocksolid light 0.9.8
clearnet tor