jdf / Processing.py-Bugs

A home for all bugs and feature requests about Python Mode for the Processing Development Environment.
41 stars 8 forks source link

Problem with RiTa timer function in python not working #217

Closed anjchang closed 6 years ago

anjchang commented 6 years ago

Hello. I'm trying to convert the RiTa examples to python. For some reason, the timer function does not get triggered. Here's the java version: import rita.*; void setup() { size(300, 300); RiTa.timer(this, 2); } void onRiTaEvent(RiTaEvent re) { print("timer"); }

Here's the python version: add_library('rita') def setup(): RiTa.timer(this, 2) def onRiTaEvent(RiTaEvent): print("timer")

Is there something I'm missing?

anjchang commented 6 years ago

Okay, I heard back from the developer of RiTa and he says he found that a thread needs to be started. But I don't know enough about python to tell him the answer about the best way to start a thread in python? https://github.com/dhowe/RiTa/issues/499

jdf commented 6 years ago

No, this isn't related to threading. The problem is that the RiTa library wants to call functions by name, which doesn't work in Python Mode. But I think the thing to do is not to use RiTa timer at all, but to instead just use standard Python threads

import threading
import time

class Timer(threading.Thread):
    def __init__(self, sleep, func):
        """ execute func(params) every 'sleep' seconds """
        self.func = func
        self.sleep = sleep
        threading.Thread.__init__(self, name="PeriodicExecutor")
        self.setDaemon(1)

    def run(self):
        while 1:
            time.sleep(self.sleep)
            self.func()

def setup():
    global t
    t = Timer(.8, doSomething)
    t.start()

def doSomething():
    print millis()
anjchang commented 6 years ago

Thanks again @jdf !