如何让两个python程序交互?

发布于 2024-07-17 10:13:04 字数 388 浏览 3 评论 0原文

我在一个程序中有一个 HTTP 服务器,在另一个程序中有我的基本应用程序。 它们都是循环,所以我不知道如何:

  1. 编写一个脚本来启动应用程序,然后启动 HTTP 服务器;
  2. 使这些程序在运行中交换数据。

这些事情通常是怎么做的? 我真的很喜欢 Python 解决方案,因为我的脚本是用 Python 编写的。

  1. 用户是否发出 http 请求来查询应用程序中的某些数据并返回结果?

  2. 应用程序是否收集数据并将其存储在某处? 应用程序和 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:

  1. Write a script that would start the app and then the HTTP server;
  2. 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.

  1. Does a user make an http request which queries the app for some data and return a result? Yes

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

过度放纵 2024-07-24 10:13:04

a) 您可以使用os.system启动应用程序:


os.system("command")

或者您可以使用subprocess模块。 更多信息请参见此处

b) 使用套接字

a) You can start applications using os.system:


os.system("command")

or you can use the subprocess module. More information here.

b) use sockets

仙气飘飘 2024-07-24 10:13:04

好吧,您可能只使用 subprocess 模块。 对于交换数据,您可能只能使用 Popen.stdin 和 Popen.stdout 流。 当然,您/可以/做到这一点的方式没有限制。 CORBADBUS, 共享内存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.

飞烟轻若梦 2024-07-24 10:13:04

在回答之前,我认为我们需要更多信息:

  1. 这里是否存在可定义的信息管道?
    1. 用户是否发出 http 请求来向应用查询某些数据并返回结果?
    2. 应用程序是否收集数据并将其存储在某处?

有一些选项取决于您实际使用它们的方式。 套接字是一种通过文件或数据库传递信息的选项。

[编辑]根据您的回复,我认为有几种方法可以做到这一点:

  1. 如果您可以从网络服务器访问应用程序的数据库,您可以轻松地从那里提取您想要的信息。 同样,这取决于您想要交换什么信息。
  2. 如果您的应用程序只需要向 http 服务器提供一些结果,您可以将它们写入 http 服务器数据库中的结果表中。
  3. 正如其他人建议的那样,使用管道或子进程直接与后台应用程序交换数据。
  4. 使用您的应用程序可以写入并且您的 http 服务器可以读取的日志文件。

还有一些问题:

  1. 这里是否需要双向通信,或者http服务器只是显示结果?
  2. 您使用什么网络服务器?
  3. 您可以使用哪些处理语言?

根据这两个部分的依赖程度,最好编写一个新应用程序来检查应用程序的数据库是否有更改(使用挂钩或轮询或其他方式)并将相关信息发布到 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:

  1. Is there a definable pipeline of information here?
    1. Does a user make an http request which queries the app for some data and return a result?
    2. Does the app collect data and store it somewhere?

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:

  1. If you can access the app's database from the web server you could easily pull the information you're after from there. Again it depends what information it is that you want to exchange.
  2. If your app just needs to give the http server some results, you could write them into a results table in the http server's db.
  3. Use pipe's or sub processes as other people have suggested to exchange data with the background app directly.
  4. Use a log file which your app can write to and your http server read from.

Some more questions:

  1. Do you need two-way communication here or is the http server just displaying results?
  2. What webserver are you using?
  3. What processing languages do you have available on it?

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.

夜雨飘雪 2024-07-24 10:13:04

根据您想要执行的操作,您可以使用 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

友欢 2024-07-24 10:13:04

也许 twisted 就是您正在寻找的

maybe twisted is what you're looking for

行雁书 2024-07-24 10:13:04

当我用 Python 编写 Web 应用程序时,我总是将 Web 服务器与后台任务保持在同一进程中。 我不知道您使用的是什么网络服务器,但我个人使用 CherryPy。 您的应用程序可以将一堆线程用作 Web 服务器,也可以将许多其他线程用作后台任务。 这样,您不需要任何带有套接字、命名管道等的复杂 IPC。相反,您只需访问共享、全局、同步的数据结构来传递信息,并且不同的模块可以直接调用彼此的函数。

编辑:为了澄清,您可以使用 线程模块 在不同的环境中运行 CherryPy 服务器线程比其他阻塞服务器。 例如:

def listener():
    sock = get_socket_from_somewhere()
    while True:
        client, addr = sock.accept()
        # send data back to client, etc

from threading import Thread
t1 = Thread(target=listener)
t1.setDaemon(True)
t1.start()

cherrypy.quickstart() # you'd need actual arguments here

此示例展示了如何在与 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:

def listener():
    sock = get_socket_from_somewhere()
    while True:
        client, addr = sock.accept()
        # send data back to client, etc

from threading import Thread
t1 = Thread(target=listener)
t1.setDaemon(True)
t1.start()

cherrypy.quickstart() # you'd need actual arguments here

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文