-
-
Notifications
You must be signed in to change notification settings - Fork 317
Named parameters in VarPyth not cleared between function calls #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I found that this happens in multithread environment: if I call python function with named parameters in one thread, and this call is not finished when I call another function in another thread without named parameters, the second functions receives named parameters from the previous call. Is VarPyth thread safe, and is my way of overcoming the problem safe? |
Python is not thread safe. Google for "Python GIL". |
I know about GIL, and all my calls are executed between |
Assumption that one can enter only one function using VarPyth interface, is incorrect. GIL lock may be released and re-acquired inside python code (during time.sleep, IO operations, in GUI message loop, etc.), so in multy-threaded application it's generally normal to have more than one function running inside the same python interpreter. |
Can you submit a patch? |
I'm not sure if the patch I currently use can cause other problems. I'll try to investigate the problem more thorougly and make a general silution. |
…yscripter#125 Supposed fix to Named parameters in VarPyth not cleared between function calls pyscripter#125 issue. The patch is very inelegant, but I cannot see any side effects. As I understand, all data from named parameters is copied to a python object prior to do the actual function call, so truncation of fNamedParams should be OK at any moment.
I don't see how your PR resolves the issue. The problem is that fNamedParams is stored as a field of TPythonVariantType (singleton class). If multiple threads use Python Variants to make calls to Python you will still have problems, Possible Solutions:
Why this flawed designDispInvoke calls DoProcedure and DoFunction which then use EvalPython to do the work. The inherited DispInvoke does not make use of or deal with Named arguments and it needs to be overridden as well as DoProcedure and DoFunction. They all have a fixed signature, so you cannot for example pass another parameter from DispInvoke to DoFunction and you need to have some other means of passing fNamedParams from DispInvoke to DoFunction and then to EvalPython. |
You are absolutely right, my code is not a resolution but a dirty workaround. However it looks like GIL will do the magic, and this code should work correct. As I understand VarPyth code, named parameters are transfered via fNamedParams to python dict object by calling PyDict_SetItemString in a loop (line 1676). If I am correct, we may SetLength(fNamedParams, 0) immidiately after this loop. I assume that due to GIL execution of the thread cannot be interrupted between setting fNamedParams and calling PyDict_SetItemString, so this should be OK. This workaround is stinky, but it should work. Sorry, I'm not experienced enough to develop a real solution for the issue. |
@mce2 Could you please test it? |
Under some circumstances (sorry, I don't have minimal code to reproduce the error) named parameters in VarPyth not cleared between function calls, and function called without named parameters may recieve parameters from previous call with named parameters. Condition check in line 1091 of VarPyth.pas seems to be excessive; in my case commenting it out solved the problem:
{if CallDesc^.NamedArgCount > 0 then }GetNamedParams;
The text was updated successfully, but these errors were encountered: