Python线程和锁

时间:2018-05-01 02:29:00

标签: python multithreading locking pyspin

我有一些代码如下:

#!/usr/bin/env python

""" test out threading with PySpin and matplotlib """

import datetime
import threading
from matplotlib import pyplot as plt

import PySpin

__LOCK = threading.Lock()

# Set up figure
__FIG = plt.figure()
__AXES = __FIG.add_axes([0, 0, 1, 1])

# Get camera
__SYSTEM = PySpin.System.GetInstance()
__CAM = __SYSTEM.GetCameras().GetByIndex(0)

def test():
    """ test thread for streaming image """

    while True:
        with __LOCK:
            print('test thread: ' + str(datetime.datetime.now()))
            image = get_image() # pylint: disable=unused-variable

def get_image():
    """ grabs image and returns numpy array """

    image = __CAM.GetNextImage()

    # Initialize image data
    image_data = None

    # Ensure image is complete
    if not image.IsIncomplete():
        # Get image data
        image_data = image.GetNDArray()

        # Release image
        image.Release()

    return image_data

def main():
    """ test """

    # Start Acquisition
    __CAM.Init()
    __CAM.BeginAcquisition()

    # Start thread
    thread = threading.Thread(target=test)
    thread.start()

    # Update plot
    while True:
        #import time
        #time.sleep(0.01)
        with __LOCK:
            print('primary thread: ' + str(datetime.datetime.now()))
            plt.pause(0.01)

    return 0

if __name__ == '__main__':
    main()

不幸的是,当我运行它时,我得到一个这样的输出:

primary thread: 2018-04-30 21:21:49.297240
primary thread: 2018-04-30 21:21:49.325118
primary thread: 2018-04-30 21:21:49.352918
primary thread: 2018-04-30 21:21:49.381198
primary thread: 2018-04-30 21:21:49.408484
primary thread: 2018-04-30 21:21:49.436476
primary thread: 2018-04-30 21:21:49.463705
primary thread: 2018-04-30 21:21:49.492506
primary thread: 2018-04-30 21:21:49.520737
primary thread: 2018-04-30 21:21:49.548624
primary thread: 2018-04-30 21:21:49.577559
primary thread: 2018-04-30 21:21:49.604856
primary thread: 2018-04-30 21:21:49.633234
test thread: 2018-04-30 21:21:49.660484
test thread: 2018-04-30 21:21:49.661107
test thread: 2018-04-30 21:21:49.661617
test thread: 2018-04-30 21:21:49.662168
test thread: 2018-04-30 21:21:49.662787
test thread: 2018-04-30 21:21:49.663385
test thread: 2018-04-30 21:21:49.664000
test thread: 2018-04-30 21:21:49.664629
test thread: 2018-04-30 21:21:49.665230
test thread: 2018-04-30 21:21:49.665864
test thread: 2018-04-30 21:21:49.666540
test thread: 2018-04-30 21:21:49.669028
test thread: 2018-04-30 21:21:49.676831
primary thread: 2018-04-30 21:21:49.683702
primary thread: 2018-04-30 21:21:49.711935
primary thread: 2018-04-30 21:21:49.739462
primary thread: 2018-04-30 21:21:49.767674
primary thread: 2018-04-30 21:21:49.795136
primary thread: 2018-04-30 21:21:49.822378
primary thread: 2018-04-30 21:21:49.849625
primary thread: 2018-04-30 21:21:49.877958
primary thread: 2018-04-30 21:21:49.905631
primary thread: 2018-04-30 21:21:49.932940
primary thread: 2018-04-30 21:21:49.960137
primary thread: 2018-04-30 21:21:49.987946
primary thread: 2018-04-30 21:21:50.015238
primary thread: 2018-04-30 21:21:50.042956
primary thread: 2018-04-30 21:21:50.070503

我不明白为什么在切换到另一个线程之前它长时间“卡在”一个线程上?我想,当一个线程有锁,另一个线程正在等待,并在另一个线程释放它时获取它,但这似乎不是这里的情况......

0 个答案:

没有答案