简体   繁体   English

父子进程之间的通信

[英]Communication between parent child processes

Im trying to create a Python 3 program that has one or more child processes. 我试图创建一个具有一个或多个子进程的Python 3程序。

The Parent process spawns the child processes and then goes on with its own buisiness, now and then I want to send a message to a specific child process that catches it and takes action. Parent进程生成子进程,然后继续自己的商业,然后我想发送一条消息给特定的子进程,捕获它并采取行动。

Also the child process need to be non locked while waiting for message, it will run a own loop maintaning a server connection and send any recived messages on to parent. 此外,子进程在等待消息时需要非锁定,它将运行一个自己的循环来保证服务器连接并将任何已接收的消息发送给父进程。

Im currently looking at multiprocessing, threading, subprocess modules in python but have not been able to find any solution. 我目前正在寻找python中的多处理,线程,子进程模块,但一直无法找到任何解决方案。

What Im trying to achive is to have a Main part of the program that interacts with the user, taking care of user inputs and presenting information to the user. 我想要实现的是让程序的主要部分与用户交互,处理用户输入并向用户呈现信息。 This will be asychronous from the child parts that talks with different servers, reciving messages from server and sending correct messages from user to server. 这将与来自不同服务器的子部件进行异步,从服务器回收消息并从用户向服务器发送正确的消息。 The child processes will then send information back to main part where they will be pressented to user 然后,子进程将信息发送回主要部分,并将其提交给用户

My questions are: 我的问题是:

  1. Am I going at this in the wrong way 我是以错误的方式去做这件事
  2. Which module would be the best to use 哪个模块最适合使用
    2.1 How would I set this up 2.1我如何设置它

See Doug Hellmann's (multiprocessing) "Communication Between Processes" . 参见Doug Hellmann(多处理) “过程间的通信” Part of his Python Module of the Week series. 他本周系列Python模块的一部分。 It is fairly simple to use a dictionary or list to communicate with a process. 使用字典或列表与进程通信相当简单。

import time
from multiprocessing import Process, Manager

def test_f(test_d):
   """  frist process to run
        exit this process when dictionary's 'QUIT' == True
   """
   test_d['2'] = 2     ## change to test this
   while not test_d["QUIT"]:
      print "test_f", test_d["QUIT"]
      test_d["ctr"] += 1
      time.sleep(1.0)

def test_f2(name):
    """ second process to run.  Runs until the for loop exits
    """
    for j in range(0, 10):
       print name, j
       time.sleep(0.5)

    print "second process finished"

if __name__ == '__main__':
    ##--- create a dictionary via Manager
    manager = Manager()
    test_d = manager.dict()
    test_d["ctr"] = 0
    test_d["QUIT"] = False

    ##---  start first process and send dictionary
    p = Process(target=test_f, args=(test_d,))
    p.start()

    ##--- start second process
    p2 = Process(target=test_f2, args=('P2',))
    p2.start()

    ##--- sleep 3 seconds and then change dictionary
    ##     to exit first process
    time.sleep(3.0)
    print "\n terminate first process"
    test_d["QUIT"] = True
    print "test_d changed"
    print "data from first process", test_d

    time.sleep(5.0)
    p.terminate()
    p2.terminate()

Sounds like you might be familiar with multi-processing, just not with python. 听起来你可能熟悉多处理,而不是python。

os.pipe will supply you with pipes to connect parent and child. os.pipe将为您提供连接父母和孩子的管道。 And semaphores can be used to coordinate/signal between parent&child processes. 信号量可用于在父进程和子进程之间进行协调/信号传递。 You might want to consider queues for passing messages. 您可能希望考虑传递消息的队列

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

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