- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlesson04_submit_no_args.py
More file actions
Latest commit
27 lines (25 loc) · 795 Bytes
/
Copy pathlesson04_submit_no_args.py
File metadata and controls
27 lines (25 loc) · 795 Bytes
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
# SuperFastPython.com
# example issuing an asynchronous task with no arguments
fromrandomimportrandom
fromtimeimportsleep
fromconcurrent.futuresimportThreadPoolExecutor
# custom function to be executed in a worker thread
deftask():
# generate a random value between 0 and 1
value=random()
# report a message
print(f'Task generated {value}')
# block for a moment to simulate work
sleep(value)
# return a new value
returnvalue
# protect the entry point
if__name__=='__main__':
# create the thread pool
withThreadPoolExecutor() asexe:
# issue an asynchronous task
future=exe.submit(task)
# get the result once the task completes
result=future.result()
# report the result
print(result)