February 2019
Intermediate to advanced
672 pages
16h 50m
English
Even if multiprocessing uses processes (with their own independent memory), it lets you define certain variables and arrays as shared memory. You can define a shared variable using multiprocessing.Value, passing its data type as a string (i integer, d double, f float, and so on). You can update the content of the variable through the value attribute, as shown in the following code snippet:
shared_variable = multiprocessing.Value('f') shared_variable.value = 0
When using shared memory, you should be aware of concurrent accesses. Imagine that you have a shared integer variable and each process increments its value multiple times. You will define a process class as follows:
class Process(multiprocessing.Process): ...