scheduler.py :  » IDE » PyPE » PyPE-2.9.1 » plugins » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » IDE » PyPE 
PyPE » PyPE 2.9.1 » plugins » scheduler.py

'''
This software is licensed under the GPL (GNU General Public License) version 2
as it appears here: http://www.gnu.org/copyleft/gpl.html
It is also included with this archive as `gpl.txt <gpl.txt>`_.
'''


import sched
import time
import traceback
import Queue
import sys
import os

USE_WX_TIMER = 1

class sobject(object):
    __slots__ = 'sched', 'function', 'args', 'kwargs', 'delay', 'priority', 'runcount', 'item'
    # an object-oriented version of the scheduler interface, with auto multi-run
    def __init__(self, sched, runcount, delay, priority, function, *args, **kwargs):
        ## print "scheduling item with delay", delay, function
        self.sched = sched
        self.function = function
        self.args = args
        self.kwargs = kwargs
        self.runcount = runcount
        self.delay = delay
        self.priority = priority
        self.item = None
        if runcount is None or runcount > 0:
            self._sched(delay, priority, self.run)
                
    def _sched(self, delay, priority, run):
        self.item = GlobalSchedule.enter(delay, priority, run, ())
        gsq = GlobalSchedule._queue[:1]
        if USE_WX_TIMER and gsq and gsq[0] is self.item:
            dt = max(int((gsq[0].time - time.time())*1000), 10)
            TIMER_INSTANCE.Stop()
            TIMER_INSTANCE.Start(milliseconds=dt, oneShot=True)
            ## print "starting timer!"

    def cancel(self):
        if self.runcount is None or self.runcount > 0:
            if self.item:
                self.sched.cancel(self.item)
            self.runcount = 0

    def run(self):
        if self.runcount is None or self.runcount > 0:
            try:
                return self.function(*self.args, **self.kwargs)
            finally:
                if self.runcount is not None:
                    self.runcount -= 1
                if self.runcount is None or self.runcount > 0:
                    self._sched(self.delay, self.priority, self.run)

    def reenter(self, delay, priority, runcount=1):
        self.cancel()
        self.runcount = runcount
        self.item = self.sched.enter(delay, self.priority, self.run, ())

    #the wx-like interface
    def Start(self, msdelay=-1, oneShot=False):
        if msdelay == -1:
            msdelay = 1000*self.delay
        if not oneShot:
            count = None
        else:
            count = 1
        ## print ''.join(traceback.format_stack()[-4:]).rstrip()
        ## print "starting", msdelay/1000.0, self.function
        self.reenter(msdelay/1000.0, 0, count)

    Stop = cancel

    def IsRunning(self):
        return self.runcount is None or self.runcount > 0

GlobalSchedule = sched.scheduler(time.time, (lambda arg:None), True)
GlobalQ = Queue.Queue()
CyclesPerSecond = 40
QUIT = 0

def PFutureCall(msdelay, priority, function, *args, **kwargs):
    return sobject(GlobalSchedule, 1, msdelay/1000.0, priority, function, *args, **kwargs)

def FutureCall(msdelay, function, *args, **kwargs):
    return PFutureCall(msdelay, 0, function, *args, **kwargs)

def ShutdownScheduler():
    global QUIT
    QUIT = 1
    def runme(*args, **kwargs):
        os._exit(0)
    PFutureCall(-1000000, -1000000, runme)

def Timer(function, *args, **kwargs):
    # We'll "schedule" it to run zero times after 1 second
    return sobject(GlobalSchedule, 0, 1, 0, function, *args, **kwargs)

def actually_run():
    global cancelled
    try:
        cancelled
    except NameError:
        cancelled = sys.modules['__main__'].cancelled
    for i in GlobalQ.get():
        if QUIT:
            break
        _1, _2, fcn, arg = i
        try:
            ## print "running function...", fcn
            fcn(*arg)
        except cancelled:
            pass
        except (SystemExit, AssertionError, KeyboardInterrupt):
            break
        except:
            import traceback
            traceback.print_exc()

def RunScheduledEvents():
    x = GlobalSchedule.getqueue(time.time(), copy=0)
    if QUIT:
        return 1
    import wx
    GlobalQ.put(x)
    if QUIT:
        return 1
    try:
        wx.CallAfter(actually_run)
    except AssertionError:
        return 1
    return 0

def _schedulethread(ok=0):
    while ok and not QUIT:
        time.sleep(1.0/max(CyclesPerSecond, 1))
        if QUIT:
            return
        if RunScheduledEvents():
            return
    if QUIT:
        return
    import threading
    x = threading.Thread(target=_schedulethread, args=(1,))
    x.setDaemon(1)
    x.start()

import sys
sys.modules['__builtin__'].FutureCall = FutureCall
sys.modules['__builtin__'].PFutureCall = FutureCall
sys.modules['__builtin__'].Timer = Timer
sys.modules['__builtin__'].ShutdownScheduler = ShutdownScheduler

if USE_WX_TIMER:
    def OnTimerDone(evt=None):
        ## print "going to run scheduled events..."
        TIMER_INSTANCE.Stop()
        if RunScheduledEvents():
            ## print "need to kill the scheduler..."
            return
        gsq = GlobalSchedule._queue[:1]
        dt = 1000
        if gsq:
            ## print "event:", gsq[0]
            dt = min(max(int((gsq[0].time - time.time())*1000), 10), 1000)
        TIMER_INSTANCE.Start(milliseconds=dt, oneShot=True)
        ## print "starting timer!"

    def _schedulethread(ok=0):
        global wx, WXAPP, TIMER_INSTANCE
        import wx
        WXAPP = wx.GetApp()
        TIMER_INSTANCE = wx.Timer(WXAPP, wx.NewId())
        WXAPP.Bind(wx.EVT_TIMER, OnTimerDone, TIMER_INSTANCE)
        TIMER_INSTANCE.Start(1, oneShot=True)
        ## print "starting timer!"
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.