同时在两个函数运行中访问同一个对象的一个属性
Access an attribute of the same object in two functions running at the same time
您好,我遇到了一个奇怪的问题,也许有人可以提供帮助,
我从 运行 2 个具有相同参数的不同函数开始,这是一个已经实例化的对象:
iotComponent.connectedSensors=sensorList
iotComponent.connectedHUIs=HUIList
Coap = multiprocessing.Process(target=runCoapSync,args=(iotComponent,))
huis = multiprocessing.Process(target=runHuis,args=(iotComponent,))
huis.start()
Coap.start()
那么这两个函数都是:
async def runCoap(iotDevice):
context = await Context.create_client_context()
sensor=iotDevice.connectedSensors[0]
while True:
await asyncio.sleep(1)
sensor.sense()
lightMsg = iotDevice.applicationInterface.createMsg( sensor, iotDevice.communicationProtocol.name)
await iotDevice.communicationProtocol.sendMsg(context,lightMsg,"light")
def runHuis(iotDevice):
print("----------------1---------------")
LCD=iotDevice.connectedHUIs[0]
while True:
LCD.alertHuman(iotDevice.connectedSensors[0].data.value)
在第一个函数中,当 sensor.sense()
被调用时,传感器数据属性中的值属性被更新。
但是在第二个函数中,iotDevice.connectedSensors[0].data.value
总是等于零。我觉得这种行为很奇怪,因为这是同一个对象。此外,如果我在第二个函数中添加一行 sensor.sense()
,该值会更新,但它与第一个函数中打印的值不同。
编辑 0:
这是 sense() 方法:
def sense(self):
pinMode(self.pinNumber, "INPUT")
lightSensorValue = analogRead(self.pinNumber)
self.data.timestamp=str(round(time.time(), 3))
self.data.value=lightSensorValue
要是有人出点子就好了!
解决方案:如已接受的答案中所述,我尝试使用线程,效果非常好:
Coap = threading.Thread(target=runCoapSync,args=(iotComponent,))
huis = threading.Thread(target=runHuis,args=(iotComponent,))
huis.start()
Coap.start()
参见 this answer. Essentially what's happening is that your data is "pickled" before being sent to the processes to have work done. When the objects are received, they're unpacked. Therefore, the objects are more cloned than passed around. Therefore, you're actually working with two separate copies of iotComponent
, which explains why you can't actually see any change happening on one even though you "know" work is being done. There might be a way to do this, given this. However, it might be better to not use Process
, but use Thread
instead, see here. The difference is that, according to this,线程更适合 I/O-bound 操作,您的传感器肯定是这样。
您好,我遇到了一个奇怪的问题,也许有人可以提供帮助, 我从 运行 2 个具有相同参数的不同函数开始,这是一个已经实例化的对象:
iotComponent.connectedSensors=sensorList
iotComponent.connectedHUIs=HUIList
Coap = multiprocessing.Process(target=runCoapSync,args=(iotComponent,))
huis = multiprocessing.Process(target=runHuis,args=(iotComponent,))
huis.start()
Coap.start()
那么这两个函数都是:
async def runCoap(iotDevice):
context = await Context.create_client_context()
sensor=iotDevice.connectedSensors[0]
while True:
await asyncio.sleep(1)
sensor.sense()
lightMsg = iotDevice.applicationInterface.createMsg( sensor, iotDevice.communicationProtocol.name)
await iotDevice.communicationProtocol.sendMsg(context,lightMsg,"light")
def runHuis(iotDevice):
print("----------------1---------------")
LCD=iotDevice.connectedHUIs[0]
while True:
LCD.alertHuman(iotDevice.connectedSensors[0].data.value)
在第一个函数中,当 sensor.sense()
被调用时,传感器数据属性中的值属性被更新。
但是在第二个函数中,iotDevice.connectedSensors[0].data.value
总是等于零。我觉得这种行为很奇怪,因为这是同一个对象。此外,如果我在第二个函数中添加一行 sensor.sense()
,该值会更新,但它与第一个函数中打印的值不同。
编辑 0: 这是 sense() 方法:
def sense(self):
pinMode(self.pinNumber, "INPUT")
lightSensorValue = analogRead(self.pinNumber)
self.data.timestamp=str(round(time.time(), 3))
self.data.value=lightSensorValue
要是有人出点子就好了!
解决方案:如已接受的答案中所述,我尝试使用线程,效果非常好:
Coap = threading.Thread(target=runCoapSync,args=(iotComponent,))
huis = threading.Thread(target=runHuis,args=(iotComponent,))
huis.start()
Coap.start()
参见 this answer. Essentially what's happening is that your data is "pickled" before being sent to the processes to have work done. When the objects are received, they're unpacked. Therefore, the objects are more cloned than passed around. Therefore, you're actually working with two separate copies of iotComponent
, which explains why you can't actually see any change happening on one even though you "know" work is being done. There might be a way to do this, given this. However, it might be better to not use Process
, but use Thread
instead, see here. The difference is that, according to this,线程更适合 I/O-bound 操作,您的传感器肯定是这样。