timer.one_shot


#1

Hi everyone,
does someone know if there’s a way to use the one_shot function, from the module Software Timers, to call a function with some parameters?
I can use it to call a fun without any parameter, like:

t=timers.timer()
t.start()
t.one_shot(delay,fun)
<p>but nothing else.<br><br>thanks<br>Sergio</p>

#2

Hi Sergio,
one_shot method do not allows to call a function with parameters, for now.
This limitation could be circumvented, for example, calling a function that use some already-defined variable (paying attention to thread safety!) :

import timers
import streams

def myfun():
print(“myfun: a =”,a)
print(“myfun: b =”,b)
a = 42
b = “changed"

streams.serial()
sleep(1000)

a = 0
b = “my string"

t = timers.timer()
t.one_shot(5000,myfun)

while True:
print(“main: a =”,a)
print(“main: b =”,b)
print(”--------”)
sleep(1000)
<br>

#3

Hi all,
with the r2.0.8 release, timers methods one_shot and interval allow to call a function passing a single argument (docs):

import timers
import streams

def myfun(myarg):
print(‘myarg[0] =’,myarg[0])
print(‘myarg[1] =’,myarg[1])

stream.serial()

arg = (42,‘spam’)
t = timers.timer()
t.one_shot(5000,myfun,arg)

#4

thanks everyone  :wink: