5

I am wanting to share multiple resources between multiple python scripts. Ideally what I am trying to do is run my main program. It contains some script wide variables. I then want to fork off the exact same program into a new shell and give it access to my script wide variables.

I am looking into multiprocessing but I am not sure if another way would be better such as using pipes?

1 Answer 1

3

You can use Value and Array from multiprocessing for shared memory across processes.

Example from multiprocessing's documentation:

from multiprocessing import Process, Value, Array

def f(n, a):
    n.value = 3.1415927
    for i in range(len(a)):
        a[i] = -a[i]

if __name__ == '__main__':
    num = Value('d', 0.0)
    arr = Array('i', range(10))

    p = Process(target=f, args=(num, arr))
    p.start()
    p.join()

    print(num.value)
    print(arr[:])

will output:

3.1415927
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
Sign up to request clarification or add additional context in comments.

6 Comments

Is it a pass by reference or pass by value? As in, if the newly created process modifies the variable, will the main process get the update?
Value and Array classes are abstractions for shared memory allocations, so yes, updates made to instances of these classes from any process will be reflected across all processes with access to the instances.
Is it possible to have this new process spawn into a new terminal window?
No, a new terminal window means a new shell, which breaks away from the multiprocessing-managed shared memory. If you need to spawn a new terminal window you would have to use subprocess.Popen and communicate with it via a pipe, with no shared memory possible.
I plan on having a client/server application. The server is going to be broken into two parts. The main program which handles all the connections and data received. Then I want to be able to open other shells, run the same program, but just receive data. So each shell gives me the ability to interact with a client that is connected to server. Would pipes be the way to do this? Would local sockets be better?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.