mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 17:47:02 +03:00
Update thread-local singleton example
This commit is contained in:
parent
f27fa60413
commit
ac150bb2e4
|
@ -25,25 +25,26 @@ example = providers.DelegatedCallable(example,
|
||||||
thread_factory = providers.Factory(threading.Thread,
|
thread_factory = providers.Factory(threading.Thread,
|
||||||
target=example)
|
target=example)
|
||||||
|
|
||||||
# Create 10 threads for concurrent execution of example():
|
if __name__ == '__main__':
|
||||||
threads = []
|
# Create 10 threads for concurrent execution of example():
|
||||||
for thread_number in xrange(10):
|
threads = []
|
||||||
|
for thread_number in xrange(10):
|
||||||
threads.append(thread_factory(name='Thread{0}'.format(thread_number)))
|
threads.append(thread_factory(name='Thread{0}'.format(thread_number)))
|
||||||
|
|
||||||
# Start execution of all create threads:
|
# Start execution of all created threads:
|
||||||
for thread in threads:
|
for thread in threads:
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
# Wait while threads would complete their work:
|
# Wait while threads would complete their work:
|
||||||
for thread in threads:
|
for thread in threads:
|
||||||
thread.join()
|
thread.join()
|
||||||
|
|
||||||
# Making some asserts (main thread):
|
# Making some asserts (main thread):
|
||||||
all_objects = set()
|
all_objects = set()
|
||||||
|
|
||||||
while not queue().empty():
|
while not queue().empty():
|
||||||
all_objects.add(queue().get())
|
all_objects.add(queue().get())
|
||||||
|
|
||||||
assert len(all_objects) == len(threads)
|
assert len(all_objects) == len(threads)
|
||||||
# Queue contains same number of objects as number of threads where thread-local
|
# Queue contains same number of objects as number of threads where
|
||||||
# singleton provider was used.
|
# thread-local singleton provider was used.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user