Sympy multiprocessing

86 views
Skip to first unread message

Paul Royik

unread,
Feb 19, 2015, 2:35:57 AM2/19/15
to sy...@googlegroups.com
I'm using sympy in my web-based project.
Specifically, I need to calculate integral.

However, I need to limit time and interrupt function.
Code, that I use.

import multiprocessing
class RunableProcessing(multiprocessing.Process):
    def __init__(self, func, *args, **kwargs):
        self.queue = multiprocessing.Queue(maxsize=1)
        args = (func,) + args
        multiprocessing.Process.__init__(self, target=self.run_func, args=args, kwargs=kwargs)

    def run_func(self, func, *args, **kwargs):
        try:
            result = func(*args, **kwargs)
            self.queue.put((True, result))
        except Exception as e:
            self.queue.put((False, e))

    def done(self):
        return self.queue.full()

    def result(self):
        return self.queue.get()

def timeout(seconds, func, *args, **kwargs):
    proc = RunableProcessing(func, *args, **kwargs)
    proc.start()
    proc.join(seconds)
    if proc.is_alive():
        proc.terminate()
        raise TimeoutException
    success, result = proc.result()
    if success:
        return result
    else:
        raise result


And here is simple part for integration
def wrapper(f,var):
    #do stuff

def int_wrapper(f, var):
   return f.integrate(var)

try:
    sol = timeout(10, wrapper, f, var)
except TimeoutException:
   print('timeout')
   try:
      sol = timeout(10, int_wrapper, f, var)
   except Exception:
      pass

However, there is weird thing.
I use django and when I access page for example with function x, it correctly outputs x^2/2, but when I access same page withour reloading server and wrapper raises timeout, for example, on f=sin(x)**120, integrate incorrrectly outputs x sin(x)**120.
I guess x under sine and integration x are different due to multiprocessing. Can somebody clarify situation? Maybe, there is problem with globals?


Aaron Meurer

unread,
Feb 19, 2015, 10:21:34 PM2/19/15
to sy...@googlegroups.com
I'd have to debug to see what is happening, but it's not too
surprising if there are issues with SymPy and multiprocessing because
SymPy has several issues with pickle
(https://github.com/sympy/sympy/search?q=pickle&ref=cmdform&state=open&type=Issues).

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/10ba58fd-f313-4419-a4e3-27bcb3687a6e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Francesco Bonazzi

unread,
Feb 20, 2015, 5:52:00 AM2/20/15
to sy...@googlegroups.com


On Friday, February 20, 2015 at 4:21:34 AM UTC+1, Aaron Meurer wrote:
because SymPy has several issues with pickle
(https://github.com/sympy/sympy/search?q=pickle&ref=cmdform&state=open&type=Issues).

What about creating a SymPy standardized type builder? That is, instead of defining custom classes in SymPy, one would use such a builder. Besides the pickle issues, it may address the var.func(*var.args) issue, and maybe provide some possible future integration with numba, when/if numba will provide C struct-like types.
Reply all
Reply to author
Forward
0 new messages