Commits

Joao Bueno  committed 8e7f2df

Multiprocessing logic wrapper in place

  • Participants
  • Parent commits f7d0c24

Comments (0)

Files changed (3)

File lelo/__init__.py

 # coding: utf-8
 # Author: João S. O. Bueno
 
-from _lelo import *
+from _lelo import paralell
 
 #

File lelo/_lelo.py

 
 from multiprocessing import Process, Queue
 
-
-a = Process()
-
 def _xecuter(queue, func, args, kwargs):
     queue.put(func(*args, **kwargs))
 
 
-#q = Queue(1)
-#a = Process(target = soma, args=(q, 2, 3) )
+class Wrapper(object):
+    def __init__(self, func):
+        self.func = func
+        self.__name__ = func.__name__
 
-#x = a.start()
-#print q.get()
+    def __call__(self, *args, **kw):
+        self.queue = Queue(1)
+        process = Process(target=_xecuter,
+            args=(self.queue, self.func,
+                  args, kw)
+            )
+        process.start()
 
+    @property
+    def _value(self):
+        if not hasattr(self, "_real_value"):
+            self._real_value = self.queue.get()
+        return self._real_value
 
+        
 
 def paralell(func):
     """

File tests/test.py

         
         self.assertEqual(queue.get(), 5)
 
+    def test_proc_wrapper_works(self):
+
+        from lelo._lelo import Wrapper
+        def sum_(a,b):
+            return a + b
+
+        wrapped = Wrapper(sum_)
+        self.assertEqual(wrapped(2,3), None)
+        self.assertEqual(wrapped._value, 5)
+        
+        
 
 if __name__ == "__main__":