Threaded and non-threaded script code

Any type of script can run threaded and/or non-threaded script functions.

Non-threaded code

The entrance to a script is always originating from callback functions which are not running threaded, and should not be blocking. This means that every time they are called, they should perform some task and then return control. If control is not returned, then CoppeliaSim will halt.

Non-threaded code is inherently synchronized with the simulation loop:

#python def sysCall_init(): sim = require('sim') def sysCall_sensing(): print('simulation time:', sim.getSimulationTime()) --lua function sysCall_init() sim = require('sim') end function sysCall_sensing() print('simulation time: ', sim.getSimulationTime()) end

Threaded code

Threaded scripts on the other hand allow running blocking code, without halting CoppeliaSim: the code is in fact preemtively interrupted and later resumed on a regular basis by CoppeliaSim, by default.

Threaded code runs non-blocking (or in non-stepping mode) by default:

#python def sysCall_init(): sim = require('sim') def sysCall_thread(): while not sim.getSimulationStopping(): print('simulation time:', sim.getSimulationTime()) --lua function sysCall_init() sim = require('sim') end function sysCall_thread() while not sim.getSimulationStopping() do print('simulation time: ', sim.getSimulationTime()) end end

This behaviour can be changed via sim.setStepping, which then requires explicit yielding via sim.step:

#python def sysCall_init(): sim = require('sim') sim.setStepping(True) # enable the stepping mode (no more preemptive yielding from CoppeliaSim) def sysCall_thread(): while not sim.getSimulationStopping(): print('simulation time:', sim.getSimulationTime()) sim.step() # resume in the next simulation step --lua function sysCall_init() sim = require('sim') sim.setStepping(true) -- enable the stepping mode (no more preemtive yielding from CoppeliaSim) end function sysCall_thread() while not sim.getSimulationStopping() do print('simulation time: ', sim.getSimulationTime()) sim.step() -- resume in next simulation step end end

Above while loop will now execute exactly once for each main simulation step and not waste time reading and printing the simulation time over and over for same the simulation step: once the current thread has yielded control back to CoppeliaSim via sim.step(), it will resume next time the thread is resumed, which is in next simulation step.

Several blocking functions are specifically meant to be running from within threaded code, e.g. sim.moveToConfig: those functions handle thread yielding for you, at the appropriate time.

Refer also to the stepping mode, and to the other thread-related API functions.