-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbench_semaphore2.py
More file actions
58 lines (42 loc) · 1.25 KB
/
bench_semaphore2.py
File metadata and controls
58 lines (42 loc) · 1.25 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
49
50
51
52
53
54
55
56
57
58
from __future__ import print_function
import time
import os
USE_GEVENT = os.environ.get('USE_GEVENT')
#NTHREADS = int(os.environ.get('NTHREADS') or 100)
COUNT = int(os.environ.get('COUNT') or 100000)
from gevent2 import semaphore as Semaphore
from gevent2 import sleep, spawn
import gevent2
print (gevent2)
a_finished = Semaphore(0)
b_finished = Semaphore(0)
log = []
def func(source, dest, finished, rec=25):
if rec > 0:
return func(source, dest, finished, rec - 1)
print ('%r started' % source)
source_id = id(source)
for _ in xrange(COUNT):
source.acquire()
log.append(source_id)
#print '%r acquired' % source
dest.release()
#print '%r finishing' % source
finished.release()
sem1, sem2 = Semaphore(0), Semaphore(0)
spawn(func, sem1, sem2, a_finished)
spawn(func, sem2, sem1, b_finished)
sleep(1)
print ('-----------------------')
timer = time.time
start = timer()
sem1.release()
a_finished.acquire()
b_finished.acquire()
result = timer() - start
per_thread = result * 1000000. / (COUNT * 2)
print ('COUNT=%s result=%s per_thread=%.2fns' % (COUNT, result, per_thread))
for index in xrange(len(log), 1):
#print index
assert log[index - 1] != log[index]
assert len(set(log)) == 2, set(log)