I have some code which looks like:
#!/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()
Unfortunately when I run it, I get an output like this:
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
What I don't understand is why is it "stuck" on one thread for a long time before it switches over to the other thread? I figured while one thread has the lock, the other would be waiting, and would acquired it when the other thread released it, but that doesn't seem to be the case here...