开发者

Communication between parent child processes

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

The Parent process spawns the child p开发者_如何学Pythonrocesses 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.

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

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


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


Sounds like you might be familiar with multi-processing, just not with python.

os.pipe will supply you with pipes to connect parent and child. And semaphores can be used to coordinate/signal between parent&child processes. You might want to consider queues for passing messages.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜