在基于trio的Python应用程序中生成进程并在进程之间通信

在基于trio的Python应用程序中生成进程并在进程之间通信,python,python-3.x,numpy,multiprocessing,python-trio,Python,Python 3.x,Numpy,Multiprocessing,Python Trio,为了在Python库上实习,我们正在研究使用该库编写带有客户机/服务器模型的HPC并行应用程序是否是一个好主意 对于异步编程和i/o,trio确实很棒 那么,我想知道怎么做 生成进程(执行CPU-GPU绑定工作的服务器) 在进程之间通信复杂的Python对象(可能包含大型numpy数组) 我在trio的文档中没有找到推荐的方法(即使这是一个好的开始) 在Python中生成进程并进行通信的一种明显方法是使用 在HPC环境中,我认为一个好的解决方案是使用MPI()。作为参考,我还必须提到() 我不知

为了在Python库上实习,我们正在研究使用该库编写带有客户机/服务器模型的HPC并行应用程序是否是一个好主意

对于异步编程和i/o,trio确实很棒

那么,我想知道怎么做

  • 生成进程(执行CPU-GPU绑定工作的服务器)
  • 在进程之间通信复杂的Python对象(可能包含大型numpy数组)
  • 我在trio的文档中没有找到推荐的方法(即使这是一个好的开始)

    在Python中生成进程并进行通信的一种明显方法是使用

    在HPC环境中,我认为一个好的解决方案是使用MPI()。作为参考,我还必须提到()

    我不知道一个人是否可以和trio一起使用这些工具,以及正确的方法是什么

    一个有趣的相关问题
    评论
    在我看来,(see)也可能是这个问题的一个很好的解决方案的一部分。

    不幸的是,截至今天(2018年7月),Trio还不支持子流程的生成和通信,也不支持MPI或其他高级进程间协调协议的任何高级包装


    这肯定是我们最终想要实现的,如果您想更详细地讨论需要实现什么,那么您可以,或者对核心子流程支持所需的内容进行概述。但是如果你的目标是在你实习的几个月内完成一些工作,老实说,你可能想考虑更成熟的HPC工具,例如

    < P>。不幸的是,截至今天(2018年7月),三人还没有支持产卵和与子过程沟通,或MPI或其他高级进程间协调协议的任何类型的高级包装器


    这肯定是我们最终想要实现的,如果您想更详细地讨论需要实现什么,那么您可以,或者对核心子流程支持所需的内容进行概述。但是如果你的目标是在实习几个月内完成一些工作,老实说,你可能会考虑更成熟的HPC工具,比如

    截至2018年年中,三人还没有做到这一点。迄今为止,您最好的选择是使用

    trio\u asyncio
    来利用asyncio对trio仍需学习的功能的支持。

    截至2018年年中,trio尚未做到这一点。迄今为止,您最好的选择是使用
    trio\u asyncio
    来利用asyncio对trio仍然需要学习的功能的支持。

    我发布了一个非常简单的代码示例,其中使用了多处理和trio(在主程序和服务器中)。它似乎起作用了

    from multiprocessing import Process, Queue
    import trio
    import numpy as np
    
    async def sleep():
        print("enter sleep")
        await trio.sleep(0.2)
        print("end sleep")
    
    def cpu_bounded_task(input_data):
        result = input_data.copy()
        for i in range(1000000-1):
            result += input_data
        return result
    
    def server(q_c2s, q_s2c):
        async def main_server():
            # get the data to be processed
            input_data = await trio.run_sync_in_worker_thread(q_c2s.get)
            print("in server: input_data received", input_data)
            # a CPU-bounded task
            result = cpu_bounded_task(input_data)
            print("in server: sending back the answer", result)
            await trio.run_sync_in_worker_thread(q_s2c.put, result)
    
        trio.run(main_server)
    
    async def client(q_c2s, q_s2c):
        input_data = np.arange(10)
        print("in client: sending the input_data", input_data)
        await trio.run_sync_in_worker_thread(q_c2s.put, input_data)
        result = await trio.run_sync_in_worker_thread(q_s2c.get)
        print("in client: result received", result)
    
    async def parent(q_c2s, q_s2c):
        async with trio.open_nursery() as nursery:
            nursery.start_soon(sleep)
            nursery.start_soon(client, q_c2s, q_s2c)
            nursery.start_soon(sleep)
    
    def main():
        q_c2s = Queue()
        q_s2c = Queue()
        p = Process(target=server, args=(q_c2s, q_s2c))
        p.start()
        trio.run(parent, q_c2s, q_s2c)
        p.join()
    
    if __name__ == '__main__':
        main()
    

    我发布了一个使用多处理和trio(在主程序和服务器中)的非常简单的代码示例。它似乎起作用了

    from multiprocessing import Process, Queue
    import trio
    import numpy as np
    
    async def sleep():
        print("enter sleep")
        await trio.sleep(0.2)
        print("end sleep")
    
    def cpu_bounded_task(input_data):
        result = input_data.copy()
        for i in range(1000000-1):
            result += input_data
        return result
    
    def server(q_c2s, q_s2c):
        async def main_server():
            # get the data to be processed
            input_data = await trio.run_sync_in_worker_thread(q_c2s.get)
            print("in server: input_data received", input_data)
            # a CPU-bounded task
            result = cpu_bounded_task(input_data)
            print("in server: sending back the answer", result)
            await trio.run_sync_in_worker_thread(q_s2c.put, result)
    
        trio.run(main_server)
    
    async def client(q_c2s, q_s2c):
        input_data = np.arange(10)
        print("in client: sending the input_data", input_data)
        await trio.run_sync_in_worker_thread(q_c2s.put, input_data)
        result = await trio.run_sync_in_worker_thread(q_s2c.get)
        print("in client: result received", result)
    
    async def parent(q_c2s, q_s2c):
        async with trio.open_nursery() as nursery:
            nursery.start_soon(sleep)
            nursery.start_soon(client, q_c2s, q_s2c)
            nursery.start_soon(sleep)
    
    def main():
        q_c2s = Queue()
        q_s2c = Queue()
        p = Process(target=server, args=(q_c2s, q_s2c))
        p.start()
        trio.run(parent, q_c2s, q_s2c)
        p.join()
    
    if __name__ == '__main__':
        main()
    

    一个简单的例子与mpi4py。。。从三人组的角度来看,这可能是一项糟糕的工作,但似乎奏效了

    from multiprocessing import Process, Queue
    import trio
    import numpy as np
    
    async def sleep():
        print("enter sleep")
        await trio.sleep(0.2)
        print("end sleep")
    
    def cpu_bounded_task(input_data):
        result = input_data.copy()
        for i in range(1000000-1):
            result += input_data
        return result
    
    def server(q_c2s, q_s2c):
        async def main_server():
            # get the data to be processed
            input_data = await trio.run_sync_in_worker_thread(q_c2s.get)
            print("in server: input_data received", input_data)
            # a CPU-bounded task
            result = cpu_bounded_task(input_data)
            print("in server: sending back the answer", result)
            await trio.run_sync_in_worker_thread(q_s2c.put, result)
    
        trio.run(main_server)
    
    async def client(q_c2s, q_s2c):
        input_data = np.arange(10)
        print("in client: sending the input_data", input_data)
        await trio.run_sync_in_worker_thread(q_c2s.put, input_data)
        result = await trio.run_sync_in_worker_thread(q_s2c.get)
        print("in client: result received", result)
    
    async def parent(q_c2s, q_s2c):
        async with trio.open_nursery() as nursery:
            nursery.start_soon(sleep)
            nursery.start_soon(client, q_c2s, q_s2c)
            nursery.start_soon(sleep)
    
    def main():
        q_c2s = Queue()
        q_s2c = Queue()
        p = Process(target=server, args=(q_c2s, q_s2c))
        p.start()
        trio.run(parent, q_c2s, q_s2c)
        p.join()
    
    if __name__ == '__main__':
        main()
    
    通信是通过
    trio完成的。在\u worker\u线程中运行\u sync\u
    so()(1)没有取消(也没有control-C支持)和(2)使用比trio任务更多的内存(但一个Python线程不使用那么多内存)

    但对于涉及大型numpy阵列的通信,我会这样做


    一个简单的例子与mpi4py。。。从三人组的角度来看,这可能是一项糟糕的工作,但似乎奏效了

    from multiprocessing import Process, Queue
    import trio
    import numpy as np
    
    async def sleep():
        print("enter sleep")
        await trio.sleep(0.2)
        print("end sleep")
    
    def cpu_bounded_task(input_data):
        result = input_data.copy()
        for i in range(1000000-1):
            result += input_data
        return result
    
    def server(q_c2s, q_s2c):
        async def main_server():
            # get the data to be processed
            input_data = await trio.run_sync_in_worker_thread(q_c2s.get)
            print("in server: input_data received", input_data)
            # a CPU-bounded task
            result = cpu_bounded_task(input_data)
            print("in server: sending back the answer", result)
            await trio.run_sync_in_worker_thread(q_s2c.put, result)
    
        trio.run(main_server)
    
    async def client(q_c2s, q_s2c):
        input_data = np.arange(10)
        print("in client: sending the input_data", input_data)
        await trio.run_sync_in_worker_thread(q_c2s.put, input_data)
        result = await trio.run_sync_in_worker_thread(q_s2c.get)
        print("in client: result received", result)
    
    async def parent(q_c2s, q_s2c):
        async with trio.open_nursery() as nursery:
            nursery.start_soon(sleep)
            nursery.start_soon(client, q_c2s, q_s2c)
            nursery.start_soon(sleep)
    
    def main():
        q_c2s = Queue()
        q_s2c = Queue()
        p = Process(target=server, args=(q_c2s, q_s2c))
        p.start()
        trio.run(parent, q_c2s, q_s2c)
        p.join()
    
    if __name__ == '__main__':
        main()
    
    通信是通过
    trio完成的。在\u worker\u线程中运行\u sync\u
    so()(1)没有取消(也没有control-C支持)和(2)使用比trio任务更多的内存(但一个Python线程不使用那么多内存)

    但对于涉及大型numpy阵列的通信,我会这样做

    您还可以查看最终似乎发布了第一个alpha版本

    它有内置的功能集中型RPC系统(很像
    trio
    ),使用TCP和
    msgpack
    (但我认为他们计划了更多的传输)。您只需直接调用其他进程中的函数,并以各种不同的方式返回结果

    这是他们的第一个例子:

    “”“
    使用以下命令从终端运行进程监视器:
    $TERM-e watch-n 0.1“pstree-a$$”\
    &python示例/parallelism/single_func.py\
    &&杀死美元!
    """
    导入操作系统
    进口拖拉机
    进口三重奏
    异步def burn_cpu():
    pid=os.getpid()
    #烧芯@~50kHz
    对于范围内的(50000):
    等待三人睡眠(1/50000/50)
    返回os.getpid()
    异步def main():
    与拖拉机异步。打开\u托儿所()作为n:
    门户=等待n.在参与者中运行(烧掉cpu)
    #在父母身上也烧橡胶
    等待烧毁cpu()
    #等待来自目标函数的结果
    pid=等待入口。结果()
    #托儿所的尽头
    打印(f“收集的子程序{pid}”)
    如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
    三人行(主)
    
    您还可以查看最终似乎发布了第一个alpha版本

    它有内置的功能集中型RPC系统(很像
    trio
    ),使用TCP和
    msgpack
    (但我认为他们计划了更多的传输)。您只需直接调用其他进程中的函数,并以各种不同的方式返回结果

    这是他们的第一个例子:

    “”“
    使用以下命令从终端运行进程监视器:
    $TERM-e watch-n 0.1“pstree-a$$”\
    &python示例/parallelism/single_func.py\
    &&杀死美元!
    """
    导入操作系统
    进口拖拉机
    进口三重奏
    异步def burn_cpu():
    pid=os.getpid()
    #烧芯@~50kHz
    对于范围内的(50000):
    等待三人睡眠(1/50000/50)
    返回os.getpid()
    异步def main():
    与拖拉机异步。打开\u托儿所()作为n:
    门户=等待n.在参与者中运行(烧掉cpu)
    #在父母身上也烧橡胶
    等待烧毁cpu()
    #等待来自的结果