py多线程,进程几种写法及扩展

常见的multiprocessing, threading, multiprocessing.dummy

这些多进程,多线程,都是没有返回结果

multiprocessing

from multiprocessing import Process, Pool
def p():
    print("p")

p1 = Process(target=p,)
p1.start()
p1.join()

#进程池
p5 = Pool(processes=3)
p5.apply_async(p)
p5.close()

threading

from threading import Thread

p2 = Thread(target=p,)
p2.start()
p2.join()

multiprocessing.dummy

from multiprocessing.dummy import Pool as ThreadPool

def md():
    pool = ThreadPool(3)
        # 在各个线程中打开url,并返回结果
    results = pool.map(p,[])
        # 关闭线程池,等待工作结束
    pool.close()
    pool.join()

md()

concurrent.futures带返回结果

多线程,多进程带返回结果

from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor

def p():
    return "p"

with ThreadPoolExecutor(max_workers=2) as executor:
    r = executor.submit(p)
    print(r.result())

如何concurrent.futures扩展写一个多线程装饰器

示例

from tomorrow import threads
@threads(5)
def p():
    return "p"

t  = p()
print(t._future._result)

tomorrow源码

rom functools import wraps

from concurrent.futures import ThreadPoolExecutor


class Tomorrow():

    def __init__(self, future, timeout):
        self._future = future
        self._timeout = timeout

    def __getattr__(self, name):
        result = self._wait()
        return result.__getattribute__(name)

    def _wait(self):
        return self._future.result(self._timeout)


def async(n, base_type, timeout=None):
    def decorator(f):
        if isinstance(n, int):
            pool = base_type(n)
        elif isinstance(n, base_type):
            pool = n
        else:
            raise TypeError(
                "Invalid type: %s"
                % type(base_type)
            )
        @wraps(f)
        def wrapped(*args, **kwargs):
            return Tomorrow(
                pool.submit(f, *args, **kwargs),  # 创建future对象
                timeout=timeout
            )
        return wrapped
    return decorator


def threads(n, timeout=None):
    return async(n, ThreadPoolExecutor, timeout)

参考:
https://github.com/hugoren/Tomorrow

你可能感兴趣的:(py多线程,进程几种写法及扩展)