Python delays, and threading.Event.wait

I’m trying to add a short delay in some Python code for toggling a CHOP parameter switch.

I’ve tried

threading.Event.wait(1)

but it throws errors.
Can any Python experts help?

Any delays will delay the entire process / freeze the timeline etc.
What problem are you trying to solve?
If a delay is unavoidable, you may consider the DAT.run() command with the delayFrames argument.

derivative.ca/wiki088/index. … ss#Methods

-Rob

Thanks. Managed to get the following to work with a Timer CHOP

[code]import time
from threading import Thread

def onCycle(timerop, segment, cycle):
print(“TIMER COMPLETE”)
t= Thread(target=myDelay)
if op(‘websocket1’).par.socketio == 1 :
op(‘websocket1’).par.socketio = 0
t.start()
return

def myDelay():
print ("sleeping 1 second from thread ")
time.sleep(1)
op(‘websocket1’).par.socketio = 1
print ("finished sleeping 1 second from thread ")[/code]

But is the thread used to execute code in another thread?
If so this is really dangerous, since our OP access is not multi threaded.
Its prone to crash at some point.

Could you not use the Timer CHOP to do this without the thread functions?

Thanks Rob
The whole point of the delay is part of a kludge to reestablish my websocket socketIO connection.

What I’m finding is that after roughly 15 minutes of running, the WebSocket DAT ALWAYS loses its connection with the nodejs-socketIO server.

To reestablish the connection I simply toggle the Socket.io Format switch from ON, to OFF, then back to ON
But I found I need a very small delay in code from the switch to OFF and back to ON for the toggle to work.
If by using a threaded function my app will become prone to crashing then I should probably look at another solution.

Would the simplest (and better) solution be using 2 TimerCHOPs, or 1 TimerCHOP and the run + delayFrames parameter?

derivative.ca/wiki088/index. … ss#Methods
If the latter, could you help with the proper syntax for the DAT run method?

I am trying n.run(delayFrames=24)

You basically just need two text DATs with the following:

text1:
#turn it on, run text2 in 15 minutes
op(‘websocket1’).par.socketio == 1
op(‘text2’).run(delayMilliSeconds = 1000 * 60 * 15)

text2:
#turn it off, run text1 in 1 second
op(‘websocket1’).par.socketio == 0
op(‘text1’).run(delayMilliSeconds = 1000 * 1)

One will start the other. however to kick start it automatically, you could have a line in an ExecuteDAT to run text1 on start.

To manage the list of delayed runs at any time:
derivative.ca/wiki088/index. … Runs_Class

This could also be done with a Timer CHOP, though you’d have to ensure it cooks every frame when not being looked at.

Ideally though we should figure out why its dropping connection. (no ideas yet)

Thanks.
So now using a Timer CHOP and 2 Text DATs similar to your suggestion.

In the first Text DAT I have

def onCycle(timerop, segment, cycle): print("TIMER COMPLETE") # grab DAT n= op('toggleDelay') if op('websocket1').par.socketio == 1 : op('websocket1').par.socketio = 0 n.run(delayFrames=2) return

And in the 2nd (called ‘toggleDelay’ I have simply

print("TOGGLE DELAY COMPLETE") if op('websocket1').par.socketio == 0 : op('websocket1').par.socketio = 1

Yes would like to know as well. I am very happy however with the increased accuracy of the Kinect v2 sensor. Hand open close gestures are now easily tracked. I had to add additional logic to filter out the false triggers but it all works great now

Oh… boy. Just remembered,
you can pulse any parameter with a frames or seconds delay:

derivative.ca/wiki088/index. … ss#Methods

op('websocket1').par.socketio.pulse(1, frames=2)

That should simplify things a bit more.

Thanks yes, will try:)

Not sure if it helps the same issue, but in order to keep the connection with the websocket alive, I find it much easier to add Interval functions on the server side.

For example, I have a node.js server with this code:

// Connection
wss.on('connection', function(ws) {
    setInterval(function () {
        ws.ping()
    }, 15000)
   
    ws.on('pong', function() {
        console.log('pong')
    })
})

In your DAT websocket callback file, this line corresponds to the pong sent back to the server (if you need to test things).

def onReceivePing(dat, contents):
	dat.sendPong(contents) # send a reply with same message
	return 

Have a look at threading.Timer. It runs your function in a new thread without using sleep().

from threading import Timer

def hello():
    print "hello, world"

t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed

The second method to delay would be using the implicit wait method:

driver.implicitly_wait(5)

The third method is more useful when you have to wait until a particular action is completed or until an element is found:

self.wait.until(EC.presence_of_element_located((By.ID, 'UserName'))

@malcomvx I’m getting insta-crashes to desktop (no crash save) using Timer whilst calling functions that use TD methods. Are you experiencing the same?