havok2063 / boolean_parser

python package for parsing a string with boolean logic
https://boolean-parser.readthedocs.io/en/latest/index.html
BSD 3-Clause "New" or "Revised" License
14 stars 7 forks source link

boolean_parser() does not appear to be thread safe #13

Open mrmessagewriter opened 1 year ago

mrmessagewriter commented 1 year ago

Using boolean_parser to handle text parsing to convert into SQLAlchemy queries, in Flask application, I was occassionally getting the error:
TypeError: Condition.__init__() missing 1 required positional argument: 'data' I checked through all of my code, to make sure everything was correctly formatting the input string. Everything I could find in my code was throughly vetted.
Then I thought since it was a multi threaded app, perhaps this issue laid in the parser itself. I presumed the parser would be creating a new instance of parser each time. It seems to be doing that using the "parse()" method to create the correct base parser.

However given the following code, it crashes almost immediately the same way:
`if name == 'main':

from boolean_parser import parse as boolean_parser
from multiprocessing.dummy import Pool

test_string ="(owner_id == 418f5230-365f-4867-9721-0bd498968932)"

test_list = [test_string for t in range(0, 1000)]

with Pool(5) as p:
    print(p.map(boolean_parser, test_list))

` The multiprocessing.dummy class is the same as using the Threading class, just makes it easy to switch back and forth.
If you were to run the list through a standard map function, it would execute correctly. If you use just the multiprocessing module it also just works. I love the functionality of this package, and I can probably get a work around figured out, but would prefer to get this working. This happens in 1.4 and 1.5 beta.

havok2063 commented 5 months ago

@mrmessagewriter is this still an issue? Sorry for the long delay in responding. Yeah I didn't have multi-threading in mind when initially creating the package. I can take a look but if you have a workaround or solution already implemented, feel free to submit a PR for review.

mrmessagewriter commented 5 months ago

Ahh well yeah the solution is to wrap multiprocess to manage a group of threads, each executing as a single unique thread. It is not a great solution, but also not part of the boolean_parser itself.