加载和访问多个ctype实例 [英] Loading and accessing multiple ctype instances

查看:72
本文介绍了加载和访问多个ctype实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些在Python中使用的现有C代码。我可以使用以下命令加载库:

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)

这将创建第二个实例,但两者似乎具有相同的内存位置(句柄相同)。我尝试将 library.dylib 复制并重命名为 library1.dylib library2.dylib ,但这不会改变它们的加载方式。问题在于,当我在 lib1 中调用函数时,会修改 lib2 中的全局变量和状态变量。例如:

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

有没有办法以保持它们包含的方式加载这些库实例?我要加载的C代码是非常大的旧版软件...我需要做些重写吗?

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?

这是我发现解决类似问题的链接问题,但对我没有太大帮助: http://www.gossamer- thread.com/lists/python/python/826703

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.

推荐答案

您已经注意到,某些操作系统拒绝在同一进程中加载​​同一DDL的多个实例。如果其他所有操作均失败,则可以使用多处理模块来分叉程序,并在不同的进程中加载​​每个DLL。 – Sylvain Leroux 15年1月27日在22:20

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

这篇关于加载和访问多个ctype实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆