Trigger event immediately

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
bryanchicken
Posts: 10
Joined: Tue Nov 03, 2009 9:28 am

Trigger event immediately

Post by bryanchicken » Thu Nov 05, 2009 9:31 am

Hi all.

This might be a really dumb question but i can't find the answer to it.

I'm writing a python script that will loop trying a certain action for 30s. Every 10 seconds or so i want to trigger an event.
Here is some basic code as its probably easier to understand

Code: Select all

while os.path.exists(eg.globals.driveLetter+":") == False:
    if i == 1 or i == 10 or i == 20:
        eg.TriggerEvent("Start_"+eg.globals.driveLetter.upper())
        print " Start_"+eg.globals.driveLetter.upper()+" "+str(i)
    elif i == 30:
        print "Breaking "+str(i)
        break
    i = i + 1    
    print "Sleeping "+str(i)
    time.sleep(1)
The print statements work exactly like i would like it to. However all the TriggerEvents get fired after the script has finished.
Eg
print
print
print
...
print
Trigger
Trigger
Trigger

Is there anyway to Trigger the event inline or call a macro from within my script and then return to the script after?

Or will i have to put all the plugin commands directly into the script?

Thanks.

User avatar
jinxdone
Plugin Developer
Posts: 443
Joined: Tue Jan 02, 2007 4:08 pm

Re: Trigger event immediately

Post by jinxdone » Thu Nov 05, 2009 11:17 am

Hi,

The problem here is the time.sleep() function, it causes the script take some time to finish and EG can't do anything in the background while its still running, it just waits for your script to finish first.

You should use a timer in these situations, this way your script will not be blocking EG from processing other things at the same time.

So for example something like..

Code: Select all

import threading, os

def someFunction(i):
    if os.path.exists(eg.globals.driveLetter+":") == False:
        eg.TriggerEvent("Start_"+eg.globals.driveLetter.upper())
        print " Start_"+eg.globals.driveLetter.upper()+" "+i
        if int(i) < 3:
            t = threading.Timer(10.0, someFunction, str(int(i) + 1))
            t.start()
        else:
            print "parameter i=" + i + " is at max value, stopping timer loop"
    else:
        print "Path found!"

someFunction('1')
For more information on the timer check the python docs for threading

-jinxdone

bryanchicken
Posts: 10
Joined: Tue Nov 03, 2009 9:28 am

Re: Trigger event immediately

Post by bryanchicken » Thu Nov 05, 2009 1:00 pm

thankyou jinxdone. That looks like it might work. :D

Post Reply