Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 11ecd42

Browse files
Create signal_betwn_threads.py
1 parent 4ca9856 commit 11ecd42

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

‎scripts/signal_betwn_threads.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import logging
2+
import threading
3+
import time
4+
5+
logging.basicConfig(level=logging.DEBUG,
6+
format='(%(threadName)-10s) %(message)s',
7+
)
8+
9+
def wait_for_event(e):
10+
"""Wait for the event to be set before doing anything"""
11+
logging.debug('wait_for_event starting')
12+
event_is_set = e.wait()
13+
logging.debug('event set: %s', event_is_set)
14+
15+
def wait_for_event_timeout(e, t):
16+
"""Wait t seconds and then timeout"""
17+
while not e.isSet():
18+
logging.debug('wait_for_event_timeout starting')
19+
event_is_set = e.wait(t)
20+
logging.debug('event set: %s', event_is_set)
21+
if event_is_set:
22+
logging.debug('processing event')
23+
else:
24+
logging.debug('doing other work')
25+
26+
27+
e = threading.Event()
28+
t1 = threading.Thread(name='block',
29+
target=wait_for_event,
30+
args=(e,))
31+
t1.start()
32+
33+
t2 = threading.Thread(name='non-block',
34+
target=wait_for_event_timeout,
35+
args=(e, 2))
36+
t2.start()
37+
38+
logging.debug('Waiting before calling Event.set()')
39+
time.sleep(3)
40+
e.set()
41+
logging.debug('Event is set')

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /