8000 Experimenting with decorators. · rishu1601/practice-python@61de19b · GitHub
[go: up one dir, main page]

Skip to content

Commit 61de19b

Browse files
committed
Experimenting with decorators.
1 parent d4759f7 commit 61de19b

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

experiments/threads2.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33
import time
44

55

6-
def count_up(num):
6+
def synchronize(func):
7+
def lock_resource(*args, **kwargs):
8+
global lock
9+
lock.acquire()
710

8-
global database, lock
11+
try:
12+
func(*args, **kwargs)
13+
finally:
14+
lock.release()
15+
16+
return lock_resource
17+
18+
19+
@synchronize
20+
def count_up(num):
921

10-
lock.acquire()
22+
global database
1123

12-
try:
13-
time.sleep(random.randrange(3))
14-
database.append(num)
15-
finally:
16-
lock.release()
24+
time.sleep(random.randrange(3))
25+
database.append(num)
1726

1827

1928
def main():

0 commit comments

Comments
 (0)
0