-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgevent_example.py
More file actions
42 lines (28 loc) · 757 Bytes
/
gevent_example.py
File metadata and controls
42 lines (28 loc) · 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from gevent import monkey
monkey.patch_all()
from gevent import spawn, sleep
from signal import signal, setitimer, ITIMER_REAL, SIGALRM
from threading import RLock
TIMER_FREQUENCY = 2
_LOCK = RLock()
counter = 0
def continuous_counting(counter_name):
global counter
while True:
_LOCK.acquire()
try:
counter += 1
finally:
_LOCK.release()
sleep(0)
def signal_handler(signum, frame):
_LOCK.acquire()
try:
print(f"signal handler has lock")
finally:
_LOCK.release()
signal(SIGALRM, signal_handler)
setitimer(ITIMER_REAL, TIMER_FREQUENCY, TIMER_FREQUENCY)
threads = [spawn(continuous_counting, i) for i in range(3)]
for thread in threads:
thread.join()