ReferenceError: Time taking statements cannot be run in this scope.

hello,everyone! I encountered the following problems in programming, how to solve it, thank you!

ReferenceError: Time taking statements cannot be run in this scope.

All the time taking commands must be called from OnRun event (or obviously functions that are originally called from OnRun).

For example you can’t call delay(5) or servo.moveJoint(0,500) from the OnSignal event.


I think this occurred inside the def OnSignal(): function.

The OnSignal function is being called during run time, so while the code execution is for example somewhere in the def OnRun(): function the Onsignal function can be called. (I think in native Python this isn’t even possible)

Things you can do in the onsignal function are thing like appending something to a list, calculate a math problem or do anything except waiting.

Waiting examples are: delay(5) or condition(lambda: something), those things you can’t add in the OnSignal function.

I hope this makes sense if not, please let me know


Ah snap, one minute late, or just what @eme said :wink:

thank you !@eme

thank you !@JobW

Probably the technical reason why simulation-time-consuming methods can’t be used in OnSignal is that those rely on the Stackless Python coroutine system to resume execution of the Python script asynchronously once the simulation has progressed.

Allowing e.g. delay(10) in OnSignal would potentially require multiple coroutines because another signal trigger can happen during that wait. Spawning a coroutine for each OnSignal execution could solve the issue, but the OnSignal handler Python code would be difficult to debug due to seemingly parallel execution and there could be a significant performance hit.

Having a single coroutine per OnSignal Python handler would be possible if any OnSignal handler executions were queued to run one after another, but this is probably not desirable behavior either.