Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a new kick-ass feature for the spooler
- Loading branch information
roberto@debian32
committed
Aug 4, 2011
1 parent
9bd3022
commit 8c1b48a
Showing
5 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from tasksconsumer import enqueue | ||
|
||
def application(env, sr): | ||
|
||
sr('200 OK', [('Content-Type','text/html')]) | ||
|
||
enqueue(queue='fast', pippo='pluto') | ||
|
||
return "Task enqueued" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from tasksconsumer import * | ||
|
||
@queueconsumer('fast', 4) | ||
def fast_queue(arguments): | ||
print "fast", arguments | ||
|
||
@queueconsumer('slow') | ||
def slow_queue(arguments): | ||
print "foobar", arguments |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from uwsgidecorators import * | ||
import Queue | ||
from threading import Thread | ||
|
||
queues = {} | ||
|
||
class queueconsumer(object): | ||
|
||
def __init__(self, name, num=1, **kwargs): | ||
self.name = name | ||
self.num = num | ||
self.queue = Queue.Queue() | ||
self.threads = [] | ||
self.func = None | ||
queues[self.name] = self | ||
|
||
|
||
@staticmethod | ||
def consumer(self): | ||
while True: | ||
req = self.queue.get() | ||
print req | ||
self.func(req) | ||
self.queue.task_done() | ||
|
||
def __call__(self, f): | ||
self.func = f | ||
for i in range(self.num): | ||
t = Thread(target=self.consumer,args=(self,)) | ||
self.threads.append(t) | ||
t.daemon = True | ||
t.start() | ||
|
||
@spool | ||
def spooler_enqueuer(arguments): | ||
if 'queue' in arguments: | ||
queue = arguments['queue'] | ||
queues[queue].queue.put(arguments) | ||
else: | ||
raise Exception("You have to specify a queue name") | ||
|
||
|
||
def enqueue(*args, **kwargs): | ||
return spooler_enqueuer.spool(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters