Giter Club home page Giter Club logo

multicpu's Introduction

multicpu (中文版)

Set the number of cpu and thread using ONE function:

result = multi_cpu(process_job, jobs, cpu_num, thread_num)

cpu_num: the number of cpu.
thread_num: the number of thread in one cpu.

Installation

Multicpu is conveniently available via pip:

pip install multicpu

or installable via git clone and setup.py

git clone [email protected]:cyh24/multicpu.git
sudo python setup.py install

Usage

The multicpu library enables you to utilize the benefits of multi-cpu and multi-threading with minimal concern about the implementation details.

"Talk is cheap, show me your performance."

Let's take a look at how simple it is to speed up an inefficient chunk of blocking code with minimal effort.


Non-IO-Intensive Function

def process_job(job):
    time.sleep(1)
    return job

jobs = [i for i in range(20)]

IO-Intensive Function

def process_job(job):
    count = 100000000
    while count>0:
        count -= 1
    return job

jobs = [i for i in range(20)]


Non-Thread

if __name__ == "__main__":
    result = []
    for job in jobs:
        result.append(process_job(job))

Multi-thread

from concurrent import futures

if __name__ == "__main__":
    result = []
    thread_pool = futures.ThreadPoolExecutor(max_workers=10)
    result = thread_pool.map(process_job, jobs)

multicpu

from multicpu import multi_cpu

if __name__ == "__main__":
    result = multi_cpu(process_job, jobs, 10, 1)

Performance

Function Non-Thread Multi-Thread(10) multicpu(10,1) multicpu(10,2)
IO 146.42 (s) 457.53 (s) 16.34 (s) 42.81 (s)
Non-IO 20.02 (s) 2.01 (s) 2.02 (s) 1.02 (s)

How Does it Work?

Feel free to read the source for a peek behind the scenes -- it's less than 60 lines of code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.