簡體   English   中英

AWS Lambda 中的 Python:多處理,完成后終止其他進程

[英]Python in AWS Lambda: multiprocessing, terminate other processes when one finished

我從另一個問題中了解到 AWS Lambda 不支持multiprocessing.Poolmultiprocessing.Queue

我還在 AWS Lambda 中研究 Python 多處理。 但我的問題是:當第一個子進程返回時,我們如何終止主進程? (所有子進程將以不同的執行時間返回)

我在這里有什么:

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

當前程序將返回,直到所有子進程完成( for parent_connection in parent_connections )。 但我想知道如何在第一個子進程完成時終止? (至少終止主進程,其他子進程 - 可以讓它繼續運行)

補充:要清楚,我的意思是第一個返回的子進程(可能不是第一個創建的子進程)。

所以 join() 循環是等待所有子進程完成的循環。

如果我們在完成第一個孩子后打破它並強行終止所有其他過程,它會對你有用

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM