父子进程之间的通信 [英] Communication between parent child processes

查看:160
本文介绍了父子进程之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个具有一个或多个子进程的Python 3程序.

Im trying to create a Python 3 program that has one or more child processes.

父进程产生子进程,然后继续其自身的繁忙性,然后我不时要向特定的子进程发送一条消息,以使其捕获并采取行动.

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.

另外,子进程在等待消息时也需要处于非锁定状态,它将运行一个自己的循环来维护服务器连接,并将接收到的所有消息发送给父进程.

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.

我目前正在查看python中的多处理,线程,子流程模块,但找不到任何解决方案.

Im currently looking at multiprocessing, threading, subprocess modules in python but have not been able to find any solution.

我试图达到的目标是让程序的主要部分与用户交互,同时注意用户输入并向用户提供信息. 这与与不同服务器进行通信,从服务器接收消息并在用户与服务器之间发送正确消息的子部分不同步. 然后,子进程将信息发送回主要部分,并在该部分将其压迫给用户

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

我的问题是:

  1. 我是用错误的方式来做这个吗
  2. 哪个模块最适合使用
    2.1我该如何设置
  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

推荐答案

请参阅Doug Hellmann(多重处理).他的每周Python模块"系列的一部分.使用字典或列表与进程通信非常简单.

See Doug Hellmann's (multiprocessing) "Communication Between Processes". Part of his Python Module of the Week series. 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()

这篇关于父子进程之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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