如何让两个python程序交互?
我在一个程序中有一个 HTTP 服务器,在另一个程序中有我的基本应用程序。 它们都是循环,所以我不知道如何:
- 编写一个脚本来启动应用程序,然后启动 HTTP 服务器;
- 使这些程序在运行中交换数据。
这些事情通常是怎么做的? 我真的很喜欢 Python 解决方案,因为我的脚本是用 Python 编写的。
用户是否发出 http 请求来查询应用程序中的某些数据并返回结果? 是
应用程序是否收集数据并将其存储在某处? 应用程序和 HTTP 服务器都使用 SQLite 数据库。 但是数据库可能不同。
I have a HTTP sever in one program and my basic application in another one. Both of them are loops, so I have no idea how to:
- Write a script that would start the app and then the HTTP server;
- Make these programs exchange data in operation.
How are these things usually done? I would really appriciate Python solutions because my scripts are written in Python.
Does a user make an http request which queries the app for some data and return a result? Yes
Does the app collect data and store it somewhere? The app and the HTTP Server both use SQLite database. However the DBs may be different.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
a) 您可以使用os.system启动应用程序:
或者您可以使用subprocess模块。 更多信息请参见此处。
b) 使用套接字
a) You can start applications using os.system:
or you can use the subprocess module. More information here.
b) use sockets
好吧,您可能只使用 subprocess 模块。 对于交换数据,您可能只能使用 Popen.stdin 和 Popen.stdout 流。 当然,您/可以/做到这一点的方式没有限制。 CORBA,DBUS, 共享内存 、DCOP,这样的例子不胜枚举。 但首先尝试简单的方法,在本例中是常规的 python 管道/流。
Well, you can probably just use the subprocess module. For the exchanging data, you may just be able to use the Popen.stdin and Popen.stdout streams. Of course, there's no limit to ways you /could/ do it. CORBA, DBUS, shared memory, DCOP, the list goes on. But try the simple way first, which in this case is regular python pipes/streams.
在回答之前,我认为我们需要更多信息:
有一些选项取决于您实际使用它们的方式。 套接字是一种通过文件或数据库传递信息的选项。
[编辑]根据您的回复,我认为有几种方法可以做到这一点:
还有一些问题:
根据这两个部分的依赖程度,最好编写一个新应用程序来检查应用程序的数据库是否有更改(使用挂钩或轮询或其他方式)并将相关信息发布到 http 服务器自己的数据库中。 这样做的优点是可以减少两个部分的紧密耦合,这通常是一件好事。
我有一个网络服务器(Apache 2),它使用 fastcgi 模块与 Django 应用程序进行通信。 查看 djangobook 中有关 fastcgi 的部分。 Apache 使用套接字(或常规 tcp)与后台应用程序 (Django) 进行通信。
[编辑 2] 糟糕 - 刚刚发现您的网络服务器本身就是一个 python 进程。 如果都是Python,那么你可以在它自己的线程中启动每个线程并将它们都传递< a href="http://docs.python.org/library/queue.html" rel="nofollow noreferrer">Queue 对象,允许两个进程以阻塞或非阻塞方式相互发送信息阻塞方式。
Before answering, I think we need some more information:
There are a few options depending on how you're actually using them. Sockets is an option or passing information via a file or a database.
[Edit] Based on your reply I think there's a few ways you can do it:
Some more questions:
Depending on how reliant the two parts can be, it might be best to write a new app to check the database of your app for changes (using hooks or polling or whatever) and post relevent information into the http server's own database. This has the advantage of leaving the two parts less closely coupled which is often a good thing.
I've got a webserver (Apache 2) which talks to a Django app using the fastcgi module. Have a look at the section in djangobook on fastcgi. Apache uses sockets (or regular tcp) to talk to the background app (Django).
[Edit 2] Oops - just spotted that your webserver is a python process itself. If it's all python then you could launch each in it's own thread and pass them both Queue objects which allow the two processes to send each other information in either a blocking or non-blocking manner.
根据您想要执行的操作,您可以使用 os.mkfifo 创建命名管道以在两个程序之间共享数据。
http://mail.python.org/pipermail/python-列表/2006年8月/568346.html
Depending on what you want to do you can use os.mkfifo to create a named pipe to share data between your two programs.
http://mail.python.org/pipermail/python-list/2006-August/568346.html
也许 twisted 就是您正在寻找的
maybe twisted is what you're looking for
当我用 Python 编写 Web 应用程序时,我总是将 Web 服务器与后台任务保持在同一进程中。 我不知道您使用的是什么网络服务器,但我个人使用 CherryPy。 您的应用程序可以将一堆线程用作 Web 服务器,也可以将许多其他线程用作后台任务。 这样,您不需要任何带有套接字、命名管道等的复杂 IPC。相反,您只需访问共享、全局、同步的数据结构来传递信息,并且不同的模块可以直接调用彼此的函数。
编辑:为了澄清,您可以使用 线程模块 在不同的环境中运行 CherryPy 服务器线程比其他阻塞服务器。 例如:
此示例展示了如何在与 Web 服务器相同的进程中的一个线程中拥有阻塞服务器(在本例中为 CherryPy,尽管它可以是任何东西)。
When I write web applications in Python, I always keep my web server in the same process as my background tasks. I don't know what web server you're using, but I personally use CherryPy. Your application can have a bunch of its threads be the web server, with however many other threads you like as background tasks. This way you don't need any kind of complex IPC with sockets, named pipes, etc. Instead you simply access shared, global, synchronized data structures to pass along information, and your different modules can directly call each others functions.
EDIT: To clarify, you can use the threading module to run your CherryPy server in different threads than your other blocking servers. For example:
This example shows how to have a blocking server in one thread in the same process as a web server (in this case CherryPy, though it could be anything).