PymateIO-Delaying: Difference between revisions
Jump to navigation
Jump to search
NEVER USE BLOCKING INSTRUCTION in the mainloop
The sleep(), sleep_ms() are BLOCKING INSTRUCTIONS.
They should never be used in your script, neither in the mainloop
| Line 10: | Line 10: | ||
{{dbox-red|<br />The '''sleep()''', '''sleep_ms()''' are '''BLOCKING INSTRUCTIONS'''.<br/>They should '''never be used''' in your script, neither in the mainloop<br /></br />}} | {{dbox-red|<br />The '''sleep()''', '''sleep_ms()''' are '''BLOCKING INSTRUCTIONS'''.<br/>They should '''never be used''' in your script, neither in the mainloop<br /></br />}} | ||
The solution for this is: | |||
* use the '''TimeoutTimer''' class | |||
* set the delay on the '''TimeoutTimer''' when the source event occurs. | |||
* check the '''TimeoutTimer.expired''' at every mainloop cycle. | |||
* Once expired, it is time to executed the delayed code. | |||
{{PYMATE-TRAILER}} | {{PYMATE-TRAILER}} | ||
Revision as of 16:56, 29 June 2026
Abstract
The "Cycle Timing" tutorial state the following:
NEVER USE BLOCKING INSTRUCTION in the mainloop
Blocking instruction locks the script execution in a waiting state... whereas critical operation may requires quick reaction from the script.
The sleep(), sleep_ms() are BLOCKING INSTRUCTIONS.
They should never be used in your script, neither in the mainloop
The solution for this is:
- use the TimeoutTimer class
- set the delay on the TimeoutTimer when the source event occurs.
- check the TimeoutTimer.expired at every mainloop cycle.
- Once expired, it is time to executed the delayed code.
PymateIO all right reserved © 2026 - Written by MCHobby for PymateIO