2016-04-20 15:23:55 +01:00
|
|
|
# test using lock to coordinate access to global mutable objects
|
|
|
|
#
|
|
|
|
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
|
|
|
|
|
2022-08-18 07:57:45 +01:00
|
|
|
import time
|
2016-04-20 15:23:55 +01:00
|
|
|
import _thread
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2016-04-20 15:23:55 +01:00
|
|
|
def fac(n):
|
|
|
|
x = 1
|
|
|
|
for i in range(1, n + 1):
|
|
|
|
x *= i
|
|
|
|
return x
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2016-04-20 15:23:55 +01:00
|
|
|
def thread_entry():
|
|
|
|
while True:
|
|
|
|
with jobs_lock:
|
|
|
|
try:
|
|
|
|
f, arg = jobs.pop(0)
|
|
|
|
except IndexError:
|
|
|
|
return
|
|
|
|
ans = f(arg)
|
|
|
|
with output_lock:
|
|
|
|
output.append((arg, ans))
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2016-04-20 15:23:55 +01:00
|
|
|
# create a list of jobs
|
|
|
|
jobs = [(fac, i) for i in range(20, 80)]
|
|
|
|
jobs_lock = _thread.allocate_lock()
|
|
|
|
n_jobs = len(jobs)
|
|
|
|
|
|
|
|
# create a list to store the results
|
|
|
|
output = []
|
|
|
|
output_lock = _thread.allocate_lock()
|
|
|
|
|
|
|
|
# spawn threads to do the jobs
|
|
|
|
for i in range(4):
|
2024-01-04 22:53:05 +00:00
|
|
|
try:
|
|
|
|
_thread.start_new_thread(thread_entry, ())
|
|
|
|
except OSError:
|
|
|
|
# System cannot create a new thead, so stop trying to create them.
|
|
|
|
break
|
2016-04-20 15:23:55 +01:00
|
|
|
|
|
|
|
# wait for the jobs to complete
|
|
|
|
while True:
|
|
|
|
with jobs_lock:
|
|
|
|
if len(output) == n_jobs:
|
|
|
|
break
|
2024-01-04 22:53:05 +00:00
|
|
|
time.sleep(0)
|
2016-04-20 15:23:55 +01:00
|
|
|
|
|
|
|
# sort and print the results
|
|
|
|
output.sort(key=lambda x: x[0])
|
|
|
|
for arg, ans in output:
|
|
|
|
print(arg, ans)
|