Creating symbols (and expressions) in different processes

22 views
Skip to first unread message

Momchil Peychev

unread,
Mar 18, 2021, 2:26:55 AM3/18/21
to sympy

Hello,

I have the following issue: when I try to create symbols (and by extension expressions) in different processes, SymPy somehow does not detect that the symbols are the same even though they have the same name and assumptions.

As an example, consider the following code snippet and the respective output:

import multiprocessing as mp
import sympy as sp

VAR_X = sp.Symbol('x', real=True, nonzero=True)

def process():
    return sp.Symbol('x', real=True, nonzero=True)

if __name__ == '__main__':
    a1 = sp.Symbol('a', real=True, nonzero=True)
    a2 = sp.Symbol('a', real=True, nonzero=True)
    print(a1, a2, a1 == a2, a1 - a2, '\n')

    pool = mp.Pool(4)
    jobs = []
    for _ in range(5):
        jobs.append(pool.apply_async(process))
    symbols = []
    for job in jobs:
        symbols.append(job.get())
    pool.close()

    for s in symbols:
        print(s, ' | ', VAR_X, ' | ', s - VAR_X, ' | ', sp.simplify(s - VAR_X))

Output:
a a True 0

x  |  x  |  -x + x  |  -x + x
x  |  x  |  -x + x  |  -x + x
x  |  x  |  -x + x  |  -x + x
x  |  x  |  -x + x  |  -x + x
x  |  x  |  -x + x  |  -x + x

Has anyone seen this before? Can anyone explain me what is going on?

Thank you,
Momchil

Oscar Benjamin

unread,
Mar 18, 2021, 8:49:41 AM3/18/21
to sympy
On Thu, 18 Mar 2021 at 06:26, Momchil Peychev <momchil...@gmail.com> wrote:
>
> I have the following issue: when I try to create symbols (and by extension expressions) in different processes, SymPy somehow does not detect that the symbols are the same even though they have the same name and assumptions.

Maybe there's a problem with pickling and unpickling the symbol.
Working in the same process creating two different symbols returns the
exact same object:

In [5]: x1 = Symbol('x')

In [6]: x2 = Symbol('x')

In [7]: x1 is x2
Out[7]: True

Perhaps pickling creates a distinct object that does not then compare
equal in some way because the equality testing uses is for symbols.

I'm not sure what causes this but I think it's a bug so can you open
an issue on GitHub?

Oscar

Momchil Peychev

unread,
Mar 18, 2021, 9:05:07 AM3/18/21
to sympy
Dear Oscar,

Thank you for the reply.

and propose to move any further discussion there.

Thanks,
Momchil
Reply all
Reply to author
Forward
0 new messages