在Linux上运行的Python脚本如何在Wine下运行的Python脚本中调用例程? [英] How would a Python script running on Linux call a routine in a Python script running under Wine?

查看:319
本文介绍了在Linux上运行的Python脚本如何在Wine下运行的Python脚本中调用例程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Linux上运行的Python(3)脚本,称为主脚本,该脚本必须从专有DLL调用例程.到目前为止,我已经使用Wine使用以下结构解决了这个问题:

I have a Python (3) script running on Linux, referred to as the main script, which has to call a routine from a proprietary DLL. So far, I have solved this with Wine using the following construct:

# Main script running on Linux
import subprocess
# [...]
subprocess.Popen('echo "python dll_call.py %s" | wine cmd &' % options, shell = True)
# [...]

脚本 dll_call.py 由Wine下安装的Windows Python(3)解释器执行.它将返回值转储到一个文件中,然后由等待的主脚本拾取该文件.如果我必须连续执行几次,它并不是完全可靠且令人痛苦的缓慢.

The script dll_call.py is executed by a Windows Python (3) interpreter installed under Wine. It dumps the return values into a file which is then picked up by the waiting main script. It's not exactly reliable and agonizingly slow if I have to do this a few times in a row.

我想一次启动脚本 dll_call.py ,提供某种类型的简单服务器,该服务器应以某种方式公开所需的例程.归根结底,我想要一个看起来像这样的主脚本:

I'd like to start the script dll_call.py once, offering some type of a simple server, which should expose the required routine in some sort of way. At the end of the day, I'd like to have a main script looking somewhat like this:

# Main script running on Linux
import subprocess
# [...]
subprocess.Popen('echo "python dll_call_server.py" | wine cmd &', shell = True)
# [...]
return_values = call_into_dll(options)

如何最好地实现这一点(如果需要速度且安全性不成问题)?

How can this be implemented best (if speed is required and security not a concern)?

感谢@jsbueno和@AustinHastings提供的答案和建议.

Thank you @jsbueno and @AustinHastings for your answers and suggestions.

对于那些有类似问题的人:受上述答案的启发,我编写了一个小的Python模块,用于从Linux上的Python调用Windows DLL.它基于IPC,介于常规Linux/Unix Python进程和基于Wine的Python进程之间.因为我在太多不同的用例/场景中都需要它,所以我将其设计为通用"

For those having similar problems: Inspired by the mentioned answers, I wrote a small Python module for calling into Windows DLLs from Python on Linux. It is based on IPC between a regular Linux/Unix Python process and a Wine-based Python process. Because I have needed it in too many different use-cases / scenarios, I designed it as a "generic" ctypes module drop-in replacement, which does most of the required plumbing automatically in the background.

示例:假设您在Linux上的Python中,已经安装了Wine,并且想要调用msvcrt.dll(Microsoft C运行时库).您可以执行以下操作:

Example: Assume you're in Python on Linux, you have Wine installed, and you want to call into msvcrt.dll (the Microsoft C runtime library). You can do the following:

import zugbruecke as ctypes
dll_pow = ctypes.cdll.msvcrt.pow
dll_pow.argtypes = (ctypes.c_double, ctypes.c_double)
dll_pow.restype = ctypes.c_double
print('You should expect "1024.0" to show up here: "%.1f".' % dll_pow(2.0, 10.0))

源代码(LGPL)文档 .它的边缘仍然有些粗糙(例如Alpha和不安全),但是它确实可以处理大多数类型的参数(包括指针).

Source code (LGPL), PyPI package & documentation. It's still a bit rough around the edges (i.e. alpha and insecure), but it does handle most types of parameters (including pointers).

推荐答案

您可以使用XMLRPC客户端和内置于Python的stdlib的服务器来执行所需的操作.只需使您的Wine-Python将所需的函数作为XMLRPC方法公开,并从任何其他Python程序对该进程进行进程间调用即可.

You can use the XMLRPC client and servers built-in Python's stdlib to do what you want. Just make your Wine-Python expose the desired functions as XMLRPC methods, and make an inter-process call from any other Python program to that.

它也适用于从CPython调用Jython或IronPython中运行的函数,也可以跨Python2和Python3调用-模块文档中包含的示例本身就足够了.只需检查一下文档即可:

It also works for calling functions running in Jython or IronPython from CPython, and also across Python2 and Python3 - the examples included in the module documentation themselves should be enough.Just check the docs: https://docs.python.org/2/library/xmlrpclib.html

如果您需要在客户端异步调用,或者在服务器站点上响应多个进程,则可以找到其他框架来构建调用-Celery还应该在保留多个不同Python的同时工作呼叫兼容性,并且在性能方面肯定足够.

If you need the calls to be asynchronous on the client side, or the server site to respond to more than one process, you can find other frameworks over which to build the calls - Celery should also work across several different Pythons while preserving call compatibility, and it is certainly enough performance-wise.

这篇关于在Linux上运行的Python脚本如何在Wine下运行的Python脚本中调用例程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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