单独传递时,多处理将对象的值而不是对象作为参数发送
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)
运行 main_script.py 不断返回 b"0".
的值
但是,当将另一个虚拟数据作为参数传递时,从站将其作为对象接收。
因此,将 main_script.py 的 #8 行修改为:
m = multiprocessing.Process(target=monitor, args=(communication_line, 6969))
并将 slave_script.py 的 #3 行修改为:
def monitor(communication_line, dummy_data_to_ignore):
不断返回(我正在努力实现的目标)
所以,我很困惑,为什么在我发送另一个数据之前它没有将 communication_line 视为一个对象?
args
参数必须是元组。 (communication_line)
不是元组。一个逗号组成一个元组。只有操作顺序需要括号。考虑 1
、(1)
、((1))
都是整数 1。但是 1,
、(1,)
和 ((1,))
都是元组。使用 args = (communication_line,)
.
或者另一个例子:
>>> x=1,
>>> type(x)
<class 'tuple'>
>>> x=(1)
>>> type(x)
<class 'int'>
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)
运行 main_script.py 不断返回
但是,当将另一个虚拟数据作为参数传递时,从站将其作为对象接收。
因此,将 main_script.py 的 #8 行修改为:
m = multiprocessing.Process(target=monitor, args=(communication_line, 6969))
并将 slave_script.py 的 #3 行修改为:
def monitor(communication_line, dummy_data_to_ignore):
不断返回(我正在努力实现的目标)
所以,我很困惑,为什么在我发送另一个数据之前它没有将 communication_line 视为一个对象?
args
参数必须是元组。 (communication_line)
不是元组。一个逗号组成一个元组。只有操作顺序需要括号。考虑 1
、(1)
、((1))
都是整数 1。但是 1,
、(1,)
和 ((1,))
都是元组。使用 args = (communication_line,)
.
或者另一个例子:
>>> x=1,
>>> type(x)
<class 'tuple'>
>>> x=(1)
>>> type(x)
<class 'int'>