24

I have some existing C code that I am working with in Python. I am able to load the library using the following commands:

library_path = '/full/path/to/my/library.dylib'
lib1 = cdll.LoadLibrary(library_path)

The problem is that I need to have multiple instances of this library, for example:

lib2 = cdll.LoadLibrary(library_path)

This creates a second instance, but both seem to have the same memory location (handles are the same). I've tried copying and renaming library.dylib to library1.dylib and library2.dylib, but this doesn't change how they load. The problem is that when I call function in lib1, global and state variables in lib2 are modified. For example:

lib1.open('/path/to/myfile')  # open a file for processing
lib1.run()   # This will do something with the file

lib2.open('/path/to/anotherfile')  # open a file for processing
lib2.run()   # This will do something with the file

lib1.close() # Closes library 1

lib2.run()   # This fails because lib1.close() also closes lib2

Is there any way to load these library instances in way that they remain 'contained'? The C code that I am trying to load is very large legacy software...do I need to do some rewriting?

Here is a link that I found addressing a similar problem, but doesn't help me that much: http://www.gossamer-threads.com/lists/python/python/826703

Any help is greatly appreciated.

Castrona
  • 493
  • 1
  • 3
  • 17
  • 5
    As you noticed, some OS refuses to load several instances of the same DDL in the same process. If everything else failed, you might use the [multiprocessing](https://docs.python.org/3/library/multiprocessing.html) module to fork your program, and load each DLL in a different process. – Sylvain Leroux Jan 27 '15 at 22:20
  • 1
    Check (newer) https://stackoverflow.com/questions/54243176/independent-cdll-library-instances-with-ctypes/54257116#54257116. The behavior is for *Lnx* (same behavior on *Win*), so I'd expect to also be the same on *OSX*. – CristiFati Aug 12 '19 at 15:14

2 Answers2

2

As you noticed, some OS refuses to load several instances of the same DDL in the same process. If everything else failed, you might use the multiprocessing module to fork your program, and load each DLL in a different process. – Sylvain Leroux Jan 27 '15 at 22:20

Nickolay
  • 31,095
  • 13
  • 107
  • 185
0

An answer given to a question on circular imports may (or may not) hold a solution to your issue.

Here is the original thread: Circular Imports Hell

This is the relevant part that you may find interesting, as originally posted by Sebastian Wozny:


Begin Excerpt:

From David Beazleys excellent talk Modules and Packages: Live and Let Die! - PyCon 2015, 1:54:00, here is a way to deal with circular imports in python:

 try:
     from images.serializers import SimplifiedImageSerializer
 except ImportError:
     import sys
     SimplifiedImageSerializer = sys.modules[__package__ + '.SimplifiedImageSerializer']

This tries to import SimplifiedImageSerializer and if ImportError is raised, because it already is imported, it will pull it from the importcache.

End Excerpt:


I hope this is helpful. -Science_1

user10637953
  • 370
  • 1
  • 10