Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
37
Task/Metered-concurrency/Python/metered-concurrency.py
Normal file
37
Task/Metered-concurrency/Python/metered-concurrency.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import time
|
||||
import threading
|
||||
|
||||
# Only 4 workers can run in the same time
|
||||
sem = threading.Semaphore(4)
|
||||
|
||||
workers = []
|
||||
running = 1
|
||||
|
||||
|
||||
def worker():
|
||||
me = threading.currentThread()
|
||||
while 1:
|
||||
sem.acquire()
|
||||
try:
|
||||
if not running:
|
||||
break
|
||||
print '%s acquired semaphore' % me.getName()
|
||||
time.sleep(2.0)
|
||||
finally:
|
||||
sem.release()
|
||||
time.sleep(0.01) # Let others acquire
|
||||
|
||||
# Start 10 workers
|
||||
for i in range(10):
|
||||
t = threading.Thread(name=str(i), target=worker)
|
||||
workers.append(t)
|
||||
t.start()
|
||||
|
||||
# Main loop
|
||||
try:
|
||||
while 1:
|
||||
time.sleep(0.1)
|
||||
except KeyboardInterrupt:
|
||||
running = 0
|
||||
for t in workers:
|
||||
t.join()
|
||||
Loading…
Add table
Add a link
Reference in a new issue