0

I learned that AWS Lambda does not support multiprocessing.Pool and multiprocessing.Queue from this other question.

I'm also working on Python multiprocessing in AWS Lambda. But my question: how do we terminate the main process when the first child process returns? (all child processes will return with different execution time)

What I have here:

import time
from multiprocessing import Process, Pipe

class run_func():
    
    number = 0

    def __init__(self, number):
        self.number = number

    def subrun(self, input, conn):
         # subprocess function with different execution time based on input.
        response = subprocess(input)
        conn.send([input, response])
        conn.close()

    def run(self):
        number = self.number
        processes = []
        parent_connections = []
        for i in range(0, number):
            parent_conn, child_conn = Pipe()
            parent_connections.append(parent_conn)
            process = Process(target=self.subrun, args=(i, child_conn,))
            processes.append(process)

        for process in processes:
            process.start()
        for process in processes:
            process.join()

        results = []
        for parent_connection in parent_connections:
            resp = parent_connection.recv()
            print(resp)
            results.append((resp[0],resp[1]))
        return results

def lambda_handler(event, context):
    starttime = time.time()
    results = []
    work = run_func(int(event['number']))
    results = work.run()
    print("Results : {}".format(results))
    print('Time: {} seconds'.format(time.time() - starttime))
    return output

The current program will return until all child processes finish (with for parent_connection in parent_connections). But I wonder how to terminate with the first child process finish? (terminate the main at least, other child processes - it's ok to leave it running)

Added: To be clear, I mean the first returned child process (may not be the first created child).

cstjh
  • 153
  • 1
  • 2
  • 8

1 Answers1

0

So the join() loop is the one which waits for all child process to complete.

If we break that after completing the first child and terminate all other process forcefully it'll work for you

class run_func():
    
    number = 0

    def __init__(self, number):
        self.number = number

    def subrun(self, input, conn):
         # subprocess function with different execution time based on input.
        response = subprocess(input)
        conn.send([input, response])
        conn.close()

    def run(self):
        number = self.number
        processes = []
        parent_connections = []
        for i in range(0, number):
            parent_conn, child_conn = Pipe()
            parent_connections.append(parent_conn)
            process = Process(target=self.subrun, args=(i, child_conn,))
            processes.append(process)

        for process in processes:
            process.start()
        for process in processes:
            process.join()
            break
        results = []
        for parent_connection in parent_connections:
            resp = parent_connection.recv()
            print(resp)
Prashanna
  • 889
  • 1
  • 8
  • 13
  • Maybe my explanation leads to some confusion, I mean the first returned process, not the first created process. How should I do with this? – cstjh Oct 02 '20 at 20:41