0

How can I send a value which in a process to another process? For example I have something like this code piece. I want to print value in xFunc. Can someone explain how can I do it? Thank you.

def yFunc():
    value = 5

def xFunc():
    print(value)

def smap(f):
    return f()

def main():
    f_x = functools.partial(xFunc)
    f_y = functools.partial(yFunc)

    with Pool() as pool:
        res = pool.map(smap, [f_x, f_y])


if __name__ == '__main__':
    main()

Edit: value is not constant number it is changing continuously.

Edit2: I found a way for my problem. Here the solution:

https://stackoverflow.com/a/58208695/16660763

2 Answers2

0

There are several ways. One way would be to use the global keyword. like this:

def yFunc():
    global value
    value = 5

def xFunc():
    print(value + 1)

yFunc()
xFunc()

Or like this:

value = 5

def yFunc():
    global value
    value = value + 1

def xFunc():
    print(value)

yFunc()
xFunc()

Another way would be to pass the variable to the function like this:

def yFunc():
    value = 5
    return value

def xFunc(x):
    print(x)

xFunc(yFunc())

Hope that's what you were asking.

d6stringer
  • 71
  • 1
  • 10
0

Python has provided some tools to realize it.

Considering Pipes, Queues, Value and Managers.

Jay
  • 738
  • 8
  • 14
  • Yes, I solved by using Managers(). Thank you. – CocoCoder41 Aug 18 '21 at 00:18
  • If you need to share "simple" values such as integers, then you should use a shared memory implementation such as a [Value](https://docs.python.org/3/library/multiprocessing.html#shared-ctypes-objects) rather than a managed type for efficiency. – Booboo Aug 19 '21 at 10:18