Permalink
Browse files

a new bunch of decorators

  • Loading branch information...
1 parent 3585bb0 commit 87f649b71b7e641094bc3c218c2e37e89d42bac8 roberto@maverick64 committed Jul 1, 2011
Showing with 72 additions and 9 deletions.
  1. +11 −6 decoratortest.py
  2. +14 −0 runningthread.py
  3. +47 −3 uwsgidecorators.py
View
@@ -34,18 +34,23 @@ def tmpmodified(num):
# spool a long running task
@spool
-def a_long_task(vars):
+def a_long_task(args):
for i in xrange(1,10):
- print(i)
+ print("%s = %d" % ( str(args), i))
time.sleep(1)
-# continuosly spool a long task
+# continuosly spool a long running task
@spoolforever
-def an_infinite_task(vars):
+def an_infinite_task(args):
for i in xrange(1,4):
- print("infinite: %d" % i)
+ print("infinite: %d %s" % (i, str(args)))
time.sleep(1)
+# run a task every hour
+@cron(59, -1, -1, -1, -1)
+def one_hour_passed(num):
+ print("received signal %d after 1 hour" % num)
-a_long_task.spool(foo='bar')
+
+a_long_task.spool({'foo':'bar'}, hello='world')
an_infinite_task.spool(foo='bar')
View
@@ -0,0 +1,14 @@
+from threading import Thread
+import time
+
+def mess():
+ while True:
+ for i in xrange(0, 100):
+ print(i)
+ time.sleep(0.1)
+
+t = Thread(target=mess)
+t.daemon = True
+t.start()
+
+print("thread started")
View
@@ -16,7 +16,9 @@ def get_free_signal():
raise Exception("No free uwsgi signal available")
def manage_spool_request(vars):
- spooler_functions[vars['ud_spool_func']](vars)
+ ret = spooler_functions[vars['ud_spool_func']](vars)
+ if not spooler_functions[vars['ud_spool_func']].ret:
+ return ret
return spooler_functions[vars['ud_spool_func']].ret
uwsgi.spooler = manage_spool_request
@@ -26,20 +28,42 @@ class spool(object):
def spool(self, *args, **kwargs):
self.f.ret = uwsgi.SPOOL_OK
- return uwsgi.spool(ud_spool_func=self.f.__name__)
+ arguments = self.base_dict
+ if len(args) > 0:
+ arguments.update(args[0])
+ if kwargs:
+ arguments.update(kwargs)
+ return uwsgi.spool(arguments)
def __init__(self, f):
if not uwsgi.spooler_pid:
raise Exception("you have to enable the uWSGI spooler to use the @spool decorator")
spooler_functions[f.__name__] = f
f.spool = self.spool
+ self.base_dict = {'ud_spool_func':f.__name__}
self.f = f
class spoolforever(spool):
def spool(self, *args, **kwargs):
self.f.ret = uwsgi.SPOOL_RETRY
- return uwsgi.spool(ud_spool_func=self.f.__name__)
+ arguments = self.base_dict
+ if len(args) > 0:
+ arguments.update(args[0])
+ if kwargs:
+ arguments.update(kwargs)
+ return uwsgi.spool(arguments)
+
+class spoolraw(spool):
+
+ def spool(self, *args, **kwargs):
+ self.f.ret = None
+ arguments = self.base_dict
+ if len(args) > 0:
+ arguments.update(args[0])
+ if kwargs:
+ arguments.update(kwargs)
+ return uwsgi.spool(arguments)
class rpc(object):
@@ -74,6 +98,26 @@ def __call__(self, f):
uwsgi.add_timer(self.num, self.secs)
return f
+class cron(object):
+
+ def __init__(self, minute, hour, day, month, dayweek, num=None):
+ if num:
+ self.num = num
+ else:
+ self.num = get_free_signal()
+ self.minute = minute
+ self.hour = hour
+ self.day = day
+ self.month = month
+ self.dayweek = dayweek
+
+ def __call__(self, f):
+ uwsgi.register_signal(self.num, "", f)
+ uwsgi.add_cron(self.num, self.minute, self.hour, self.day, self.month, self.dayweek)
+ return f
+
+
+
class rbtimer(object):
def __init__(self, secs, num=None):

0 comments on commit 87f649b

Please sign in to comment.