Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python线程和锁_Python_Multithreading_Locking_Pyspin - Fatal编程技术网

Python线程和锁

Python线程和锁,python,multithreading,locking,pyspin,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 = _

我有一些代码如下所示:

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

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

:-(@cᴏʟᴅsᴘᴇᴇᴅ 因此,我不想在这里听起来太消极,但是如果发生这种情况,在python中使用带锁的线程库有什么意义呢?它看起来几乎毫无用处。事实上,他们正在努力在即将发布的python版本中消除它。因为线程对于IO绑定的并发仍然很有用。这与“无用”有很大不同-你需要知道适合这项工作的工具。这看起来很愚蠢,但我认为我已经找到了一个“解决方案”,在释放互斥锁后短暂暂停。这可能会让其他线程有机会获得锁。