Kepler 186
Kepler 186

Reputation: 369

Multiprocessing sends value of the object instead of the object as an argument when passed alone

main_script.py

import multiprocessing
import ctypes
from os import getpid

from slave_script import monitor

communication_line = multiprocessing.Array(ctypes.c_char, b"0")
m = multiprocessing.Process(target=monitor, args=(communication_line))
m.start()
m.join()

slave_script.py

from time import sleep

def monitor(communication_line):

    while 1:
        print(type(communication_line))
        sleep(2)

running main_script.py keeps returning <class 'bytes'> with a value of b"0".

BUT, when passing another dummy data as an argument, the slave receives it as an object.

So, modifying main_script.py 's #8 line to:

m = multiprocessing.Process(target=monitor, args=(communication_line, 6969))

And modifying slave_script.py 's #3 line to:

def monitor(communication_line, dummy_data_to_ignore):

keeps returning (what I am trying to achieve) <class 'multiprocessing.sharedctypes.SynchronizedString'>

So, I am confused, why didn't it see the communication_line as an object until I sent another data alongside with it?

Upvotes: 1

Views: 29

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177600

The args parameter must be a tuple. (communication_line) is not a tuple. A comma makes a tuple. Parentheses are only needed for order of operations. Consider 1, (1), ((1)) are all integer one. But 1,, (1,) and ((1,)) are all tuples. Use args = (communication_line,).

Or another example:

>>> x=1,
>>> type(x)
<class 'tuple'>
>>> x=(1)
>>> type(x)
<class 'int'>

Upvotes: 1

Related Questions