Multicpu
Multi-CPU, Multi-Thread. Implemented in Python.
Install / Use
/learn @cyh24/MulticpuREADME
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. <br> 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 git@github.com: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.
Related Skills
node-connect
351.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
351.8kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
