-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve handling of timeouts and cameras that stop responding
Add a cancel_all_and_flush() function to clear out and cancel any jobs on the camera queue. This will allow the stop() function to operate when it would otherwise have got queued behind a job that had hung. Trying to access the result of any such cancelled job will result in a CancelledError. The wait parameter of all the "capture" functions is extended so that you can also supply an integer, being the time in seconds after which it will produce a TimeoutError if it has not finished. Signed-off-by: David Plowman <[email protected]>
- Loading branch information
1 parent
8193557
commit 9a988a3
Showing
5 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/usr/bin/python3 | ||
|
||
import time | ||
|
||
from picamera2 import CancelledError, Picamera2, TimeoutError | ||
|
||
# At 2 fps should take over 3s to see the first frame. | ||
controls = {'FrameRate': 2} | ||
|
||
with Picamera2() as picam2: | ||
config = picam2.create_preview_configuration(controls=controls) | ||
picam2.start(config) | ||
t0 = time.monotonic() | ||
|
||
# Test that we time out correctly, and that we can cancel everything so | ||
# that we stop quickly. | ||
try: | ||
array = picam2.capture_array(wait=1.0) | ||
except TimeoutError: | ||
print("Timed out") | ||
else: | ||
print("ERROR: operation did not time out") | ||
|
||
t1 = time.monotonic() | ||
if t1 - t0 > 2.0: | ||
print("ERROR: time out appears to have taken too long") | ||
|
||
picam2.cancel_all_and_flush() | ||
picam2.stop() | ||
t2 = time.monotonic() | ||
print("Stopping took", t2 - t1, "seconds") | ||
if t2 - t1 > 0.1: | ||
print(f"ERROR: stopping took too long ({t2-t1} seconds)") | ||
|
||
with Picamera2() as picam2: | ||
config = picam2.create_preview_configuration(controls=controls) | ||
picam2.start(config) | ||
t0 = time.monotonic() | ||
|
||
# Test that we can cancel a job and get a correct CancelledError. | ||
job = picam2.capture_array(wait=False) | ||
picam2.cancel_all_and_flush() | ||
|
||
try: | ||
array = job.get_result() | ||
except CancelledError: | ||
print("Job was cancelled") | ||
else: | ||
print("ERROR: job was not cancelled") | ||
|
||
t1 = time.monotonic() | ||
if t1 - t0 > 0.5: | ||
print("ERROR: job took too long to cancel") |