As we already know, each process has its own memory allocation. So, if a variable is declared in one process, then we can't update it in another process. Even if we declare a variable as global variable, it will be defined only for a process. Suppose main program is process 1 and calculation of squares of given numbers is the process 2. An empty list named result is declared in both the processes. If we append the squares of numbers to result list in process 2 then, it will not update the result list declared in process 1 as both of these lists have different memory addresses. So, we will create a shared memory which can be used by both the processes. Below is the syntax to define shared Array, Value and Queue. import multiprocessing ... arr = multiprocessing.Array( data_type, size ) val = multiprocessing.Value( data_type, numerical_value ) q = multiprocessing.Queue( ) ... All these memories can be shared in another process by passing them as argument in the process. Here updating the value of these variables in any process will update their value in other process too.
To understand the use of this concept check links below: github link 1 github link 2
0 Comments
Leave a Reply. |
AuthorVinit is a computer scientist and roboticist. His research focuses on making machines intelligent. Categories |