-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlesson05_ctypes.py
More file actions
29 lines (27 loc) · 846 Bytes
/
Copy pathlesson05_ctypes.py
File metadata and controls
29 lines (27 loc) · 846 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
28
29
# SuperFastPython.com
# example of shared ctype accessed in multiple processes
from random import random
from multiprocessing import Value
from multiprocessing import Process
# custom function to be executed in a child process
def task(shared_var):
# generate a single floating point value
generated = random()
# store value
shared_var.value = generated
# report progress
print(f'Wrote: {shared_var.value}', flush=True)
# protect the entry point
if __name__ == '__main__':
# create shared variable
variable = Value('f', 0.0)
# create a child process process
process = Process(target=task, args=(variable,))
# start the process
process.start()
# wait for the process to finish
process.join()
# read the value
data = variable.value
# report the value
print(f'Read: {data}')