-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_function.py
More file actions
49 lines (42 loc) · 1.01 KB
/
task_function.py
File metadata and controls
49 lines (42 loc) · 1.01 KB
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
41
42
43
44
45
46
47
48
import time
import queue
from multiprocessing import current_process
'''
Queue
'''
def queue_task(task_queue, number):
while True:
try:
task = task_queue.get_nowait()
except queue.Empty:
break
else:
print(current_process().name, "run " + task)
time.sleep(5)
'''
Pool
'''
def pool_task(params):
if params%2 == 0:
return params
else:
return 0
'''
Lock (with shared counter)
* lock - promise that counter will be 40
* no lock - not promise that conter will be 40 (he cab be less)
'''
def lock_task(counter, lock):
for _ in range(10):
lock.acquire()
try:
counter.value += 1
print(current_process().name, " INC - ", str(counter.value))
time.sleep(2)
finally:
lock.release()
def no_lock_task(counter, lock):
for _ in range(10):
counter.value += 1
print(current_process().name, " INC - ", str(counter.value))
time.sleep(2)