简体   繁体   English

在python中的进程之间传输数据

[英]transferring data between processes in python

I'm not new to python but still looking for the best appropriate way of sharing string-formatted data between processes. 我对python并不陌生,但仍在寻找在进程之间共享字符串格式数据的最佳方法。

Currently I have 4 processes (1 parent + 3 Childs in multiprocessing), parent is fetching data from long poll server and after few checks sends data to mysql server. 目前,我有4个进程(1个父进程+ 3个多进程子进程),父进程正在从长轮询服务器中获取数据,并且经过几次检查后才将数据发送到mysql服务器。

Then Childs (1 for each kind of tasks) are processing the data in the way I need to. 然后,Childs(每种任务为1)正在按照我需要的方式处理数据。

Everything works fine for now, my data is securely stored and always in access, especially if I need to debug or implement new features, but after all it works slower with project growth (as for web app - not good). 目前一切正常,我的数据已安全存储并可以随时访问,尤其是在我需要调试或实现新功能的情况下,但毕竟随着项目的增长,它的工作速度会变慢(对于Web应用程序-不好)。 As you might assume, kind of data stored in db is list or its objects. 您可能会假设,存储在db中的数据类型是list或其对象。 I know that there are some problems in python with transferring data between processes ('d be better to say, software restrictions)... 我知道python在进程之间传输数据存在一些问题(最好是软件限制)...

I was thinking about temporary storing data in JSON or simple txt files, but concurrency wouldn't allow me to do that. 我当时正在考虑将数据临时存储在JSON或简单的txt文件中,但是并发不允许我这样做。 Also I could try using sockets, but is it worth to start callback server for such a purpose? 我也可以尝试使用套接字,但是为此目的启动回调服务器是否值得?

Asynchronous thing didn't work for me either. 异步的东西对我也不起作用。 So what are my options in this case? 那么在这种情况下我有什么选择呢? I don't need to loose any piece of data, just as I need to keep it working fast. 我不需要丢失任何数据,就像我需要使其保持快速运转一样。 Any other measures are welcome :) 任何其他措施,欢迎:)

ps most of related topics are outdated or didn't answer my question, because I'm sure that the way I'm working with data isn't best so far. ps大多数相关主题已经过时或没有回答我的问题,因为我确信到目前为止,我处理数据的方式并不是最好的。

There're many IPC ways(socket, shared memory, pipe, FIFO...), I think you can try pipe , it is simple. IPC有很多方法(套接字,共享内存,管道,FIFO ...),我认为您可以尝试使用pipe ,这很简单。 see the example below: 请参阅以下示例:

import os, sys


def child(prefix, r, w):
    os.close(w)
    print(prefix + ':read')
    s = os.read(r, 1024)
    print(prefix + ':got:' + s)
    print(prefix + ':close')
    os.close(r)
    print(prefix + ':exit')
    sys.exit(0)


r0, w0 = os.pipe()
r1, w1 = os.pipe()
r2, w2 = os.pipe()

if os.fork():
    if os.fork():
        if os.fork():
            os.close(r0)
            os.close(r1)
            os.close(r2)
            print('p:write')
            os.write(w0, 'hello world')
            os.write(w1, 'hello world')
            os.write(w2, 'hello world')
            print('p:close')
            os.close(w0)
            os.close(w1)
            os.close(w2)
            print('p:exit')
            sys.exit(0)
        else:
            child('c0', r0, w0)
    else:
        child('c1', r1, w1)
else:
    child('c2', r2, w2)

The multiprocessing module provides some tools to do this: https://docs.python.org/3.6/library/multiprocessing.html#exchanging-objects-between-processes multiprocessing模块提供了执行此操作的一些工具: https : //docs.python.org/3.6/library/multiprocessing.html#exchanging-objects-between-processes

Specifically take a look at Queue . 具体来看一下Queue Any process can push messages onto or pop messages from the queue. 任何进程都可以将消息推送到队列中或从队列中弹出消息。 So for example, a parent could push tasks, the children wait for tasks and get them on a first-come-first-served basis. 因此,例如,父母可以执行任务,孩子可以等待任务,并以先到先得的方式获得任务。 If one child is busy working on that task, the next child will get it. 如果一个孩子正忙于完成该任务,则下一个孩子会接受。

For example, if you needed your child processes to work on data fetched by your parent process, you could have something like this: 例如,如果您需要子进程来处理父进程获取的数据,则可能会有以下内容:

from multiprocessing import Process, Queue

def do_thing(q):
    data = q.get()  # Get data queued by the parent process
    # do something with data

if __name__ == '__main__':
    q = Queue()
    p = Process(target=do_thing, args=(q,))
    p.start()
    while True:
        data = get_data_from_server()
        q.put(data)  # Queue data for a child server

If you want something a bit more basic, you could use a Pipe , which allows two-way data transfer between processes. 如果您想要更基本的东西,可以使用Pipe ,它允许在进程之间进行双向数据传输。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM