回调适用于多线程,但不能适用于多进程

时间:2018-09-21 01:44:44

标签: python multithreading callback multiprocessing

我有一个修补程序IMU,我正在使用它们的库,该库利用回调来输出数据。我已经实现了一种多线程方法,该方法可以很好地工作,但是现在我想使其变为多进程,以获得更好的并发性。多进程可以很好地启动startIMUData函数,但不会调用myIMUCallback。

def myIMUCallback():
    print("callback called!")

# Function to start recording IMU dimport serialata via callback function above
def startIMUData(q):
    print("Starting IMU!")

    HOST = "localhost"
    PORT = 4223
    UID = "6Dcx3Y" # Change XXYYZZ to the UID of your IMU Brick 2.0

    ipcon = IPConnection() # Create IP connection
    imu = BrickIMUV2(UID, ipcon) # Create device object

    ipcon.connect(HOST, PORT) 

    # myIMUCallback is NOT being called in process version
    imu.register_callback(imu.CALLBACK_ALL_DATA, myIMUCallback)

    imu.set_all_data_period(100)

if __name__ == "__main__":

    q = Queue()   
    # This works
    threadIMU = Thread(target = startIMUData, args = (q, ))
    threadIMU.start()
    threadIMU.join()

    #This doesn't work, calls startIMUData fine but ignores myIMUCallback
    processIMU = multiprocessing.Process(target=startIMUData, args=(q, ))
    processIMU.start()
    processIMU.join()

0 个答案:

没有答案