Skip to content

Instantly share code, notes, and snippets.

@nlpjoe
Last active March 13, 2018 07:04
Show Gist options
  • Save nlpjoe/bce205c17231a98c8512ade784bc9715 to your computer and use it in GitHub Desktop.
Save nlpjoe/bce205c17231a98c8512ade784bc9715 to your computer and use it in GitHub Desktop.
[CPU任务并发代码]进程池#python

普通多线程:

    def __init__(self, name_2_id_path, triple_path):

        self.name_2_id_path = name_2_id_path
        self.triple_path = triple_path
        self.entity_2_id_set = defaultdict(dict)
        self.triple_set = defaultdict(dict)
        t1 = Thread(target=self.load_name_2_id)
        t2 = Thread(target=self.load_triple)
        t1.start()
        t2.start()
        t1.join()
        t2.join()

其他类型:

import sys
import time
from concurrent import futures
from random import randrange
from arcfour import arcfour

JOBS = 12
SIZE = 2**18

KEY = b"'Twas brillig, and the slithy toves\nDid gyre"
STATUS = '{} workers, elapsed time: {:.2f}s'


def arcfour_test(size, key):
    in_text = bytearray(randrange(256) for i in range(size))
    cypher_text = arcfour(key, in_text)
    out_text = arcfour(key, cypher_text)
    assert in_text == out_text, 'Failed arcfour_test'
    return size


def main(workers=None):
    if workers:
        workers = int(workers)
    t0 = time.time()

    with futures.ProcessPoolExecutor(workers) as executor:
        actual_workers = executor._max_workers
        to_do = []
        for i in range(JOBS, 0, -1):
            size = SIZE + int(SIZE / JOBS * (i - JOBS/2))
            job = executor.submit(arcfour_test, size, KEY)
            to_do.append(job)

        for future in futures.as_completed(to_do):
            res = future.result()
            print('{:.1f} KB'.format(res/2**10))

    print(STATUS.format(actual_workers, time.time() - t0))

if __name__ == '__main__':
    if len(sys.argv) == 2:
        workers = int(sys.argv[1])
    else:
        workers = None
    main(workers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment