Henning Schröder avatar Henning Schröder committed fa79fb3

worker tests in own file

Comments (0)

Files changed (2)

 
 class WorkerProcess(Process):
 
+    """
+    class spawns a sub-process
+    communication goes over three queues
+     * input queue: to accept requests
+     * output queue: to send ressponses
+     * control queue: to kill process
+
+    select is used to detect events
+    """
 
     def __init__(self):
         super(WorkerProcess, self).__init__()
 
     def listen_results(self, timeout=2):
         while not self.killed:
-            (output, [], []) = select.select(
+            (out_reader, _, _) = select.select(
                 [self.outq._reader], [], [], timeout)
             try:
                 job_id, error, result = self.outq.get(block=False)
     def run(self):
         pid = os.getpid()
         while 1:
-            (input, [], []) = select.select(
+            (inq, outq, ctrlq) = select.select(
                 [self.inq._reader, self.ctrlq._reader], [], [])
-            if input and self.ctrlq._reader in input:
+            if inq and self.ctrlq._reader in inq:
                 return
             job = self.inq.get()
-            job_id, name, args, kwargs = job
+            job_id, function, args, kwargs = job
             try:
-                if callable(name):
-                    result = name(*args, **kwargs)
+                if callable(function):
+                    result = function(*args, **kwargs)
                 else:
-                    result = self.functions[name](*args, **kwargs)
+                    result = self.functions[function](*args, **kwargs)
                 error = None
             except Exception, exc:
                 result = None
                 error = exc
                 print >>sys.stderr, "[Worker] Error executing job %s: %s" % (
-                    name, error)
+                    function, error)
                 import traceback
                 traceback.print_exc()
             if job_id != 0:
                 self.outq.put((job_id, error, result))
 
 
-    def execute(self, name, *args, **kwargs):
+    def execute(self, function, *args, **kwargs):
         callback = kwargs.pop("callback", None)
         if callback is None:
             job_id = 0
             self.counter += 1
             job_id = self.counter
             self.callbacks[job_id] = callback
-        job = (job_id, name, args, kwargs)
+        job = (job_id, function, args, kwargs)
         self.inq.put(job)
 
 
 
 
 
-class EventLoop(object):
-
-    def __init__(self):
-        self.mesages = []
-
-
-    def post(self, msg):
-        self.messages.append(msg)
-
-
-    def poll(self):
-        msg = self.messages.pop(0)
-
-
-if __name__ == "__main__":
-    def cb(cid, result):
-        print "callback", cid, result
-
-    def bla(*args):
-        print "BLABLA", args
-        raise RuntimeError("Error")
-
-    w = WorkerProcess()
-    w.register(bla)
-    w.start()
-    for i in range(10):
-        w.execute("bla", callback=lambda r: cb(i, r))
-
-    w.execute(bla, "stdout test")
-    print "wating"
-    while w.is_busy():
-        break
-
-    print "done"

tests/test_worker.py

+# -*- coding: utf-8 -*-
+from pycode.worker import WorkerProcess
+
+def cb(cid, result):
+    print "callback", cid, result
+
+
+def bla(*args):
+    print "BLABLA", args
+    raise RuntimeError("Error")
+
+
+def test_worker():
+    w = WorkerProcess()
+    w.register(bla)
+    w.start()
+    for i in range(10):
+        w.execute("bla", callback=lambda r: cb(i, r))
+
+    w.execute(bla, "stdout test")
+    print "wating"
+    while w.is_busy():
+        break
+
+    print "done"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.