Python Threading и блокировки - PullRequest
0 голосов
/ 01 мая 2018

У меня есть код, который выглядит так:

#!/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

Чего я не понимаю, так это почему он "застревает" в одном потоке на долгое время, прежде чем он переключается на другой поток? Я полагал, что в то время как один поток имеет блокировку, другой будет ожидать и получит ее, когда другой поток освободит ее, но здесь это не так ...

...