python out_of_process.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python out_of_process.py相关的知识,希望对你有一定的参考价值。

def out_of_process(func):
    from types import GeneratorType
    GENERATOR = b'#GENERATOR#'
    DONE = b'#DONE#'

    def runner(conn, *args, **kwargs):
        return_value = func(*args, **kwargs)
        if isinstance(return_value, GeneratorType):
            conn.send(GENERATOR)
            for val in return_value:
                conn.put(val)
            conn.send(DONE)
        else:
            conn.put(return_value)
        conn.close()

    def multi_value(proc, return_queue):
        while True:
            value = return_queue.get()
            if isinstance(value, bytes) and value == DONE:
                break
            yield value

    def wrapper(*args, **kwargs):
        return_queue = Queue()
        args = (return_queue,) + args

        proc = Process(target=runner, args=args, kwargs=kwargs)
        proc.start()

        return_value = return_queue.get()

        if isinstance(return_value, bytes) and return_value == GENERATOR:
            return multi_value(proc, return_queue)
        else:
            proc.join()
            return return_value

    return wrapper
 

以上是关于python out_of_process.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python