Search code examples
pythonpython-multiprocessing

Simply way to pass variables between processes?


There's something I've been looking for a long time to solve, and can't find.

How can I pass variables between python processes, without using Queue or Pipe?

import multiprocessing

string = "hi"

def my_process():
  global string
  string = "success!" # but its only localy...


multiprocessing.Process(target=my_process).start()

I am looking for a way to change a variable from different scripts or processes...


Solution

  • Finally,

    I created a package for Python to solve this problem.


    Install Guli from PIP.

    $ pip install guli
    

    Guli doesn't require installing any additional PIP package.

    With the package you can

    Guli can be used to pass between different Python scripts, between many processes or at the same script. pass variables between main Process and another (Multiprocess) Process.

    • Pass variables between different Python scripts.
    • Pass variables between 'Main Process' and another (Multiprocess) Process.
    • Use variables at the same script.
    • Create / Delete / Edit - GuliVariables.

    Example

    import guli
    import multiprocessing
    
    string = guli.GuliVariable("hello").get()
    print(string) # returns empty string ""
    
    def my_function():
      ''' change the value from another process '''
      guli.GuliVariable("hello").setValue(4)
    
    multiprocessing.Process(target=my_function).start()
    
    import time
    time.sleep(0.01) # delay after process to catch the update
    
    
    string = guli.GuliVariable("hello").get()
    print(string) # returns "success!!!"
    

    Hope I solved the problem for many people!