Issues with replacing Python extension module while executing Python script

I am trying to solve the following problem: Let's say I have a Python script (let him call it Test.py) that uses the C ++ extension module (done via SWIG, call the Example module). I have Test.py, Example.py and _Example.so in the same directory.

Now, in the middle of running Test.py, I want to make changes to my Example module, recompile (which will overwrite the existing .so) and use the command to gracefully stop Test.py, which is still using the old version of the module (Test.py does some a cleanup that uses some of the things that are defined in the Example module), and then run it again using the new version of the module. Gracefully stopping Test.py and THEN, recompiling the module in my case is not an option.

The problem is that as soon as _Example.so is overwritten and Test.py tries to access something defined in the Example module (with a competent stop), I get a segmentation error. One solution to this is to explicitly specify the Example module by adding the version number at the end, but I was wondering if there was a better solution (I don't want to import Example_1_0)?

+3
source share
2 answers

When running Test.py, you can copy the example files. * in the temp folder unique to this instance (look at tempfile.mkdtemp, it can create safe unique folders), add this to sys.path and then import the example; and when Test.py displays, delete this folder (shutils.rmtree) during the cleanup phase.

, Test.py Example, , .

Example. *, , Test.py, . .

+2

(Example_1_0), Test.py , _Example.so Test.py.


Edit:

, stack/generator/symlink, , " " _Example.so:

script, Test.py . Test.py _ExampleXXX.so . .so - refcount , , _Example.so.

,

while True:
    #Do stuff
    for p in myprocesses:
        retcode = p.poll() # Set to [None][1] if the process hasn't finished
        # Do something with the return code

.

0

Source: https://habr.com/ru/post/1749474/


All Articles