回调无法从线程Python工作

时间:2018-07-20 22:06:02

标签: python multithreading callback

我有一个修补程序IMU,我正在使用它们的库,该库利用回调来输出数据。我想在单独的线程上完成所有操作,但是在尝试时未调用回调函数。我在做什么错了?

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


# Function to start recording IMU data via callback function
def startIMUData():
    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) # Connect to brickd
    # Don't use device before ipcon is connected

    # Register all data callback to function cb_all_data
    imu.register_callback(imu.CALLBACK_ALL_DATA, myIMUCallback)

    # Set period for all data callback to 0.1s (100ms)
    imu.set_all_data_period(100)

# This doesn't work, callback is not called
threadIMU = Thread(target = startIMUData, args = ())
threadIMU.daemon = True
threadIMU.start

# This works
startIMUData()

1 个答案:

答案 0 :(得分:0)

您需要threadIMU.start()而不是threadIMU.start