简体   繁体   English

Python多处理方式变慢

[英]Python Multiprocessing way slower

I created a multiproc function which is slower than a monoprocess 我创建了一个multiproc函数,它比单进程慢

for n in range(nombre_de_cycles):
    debut = time.time()
    paris.reveil_multiproc(2)
    duree = time.time() - debut
    print((n, duree), end=",")

gives : 给出:

(0, 13.04754900932312),(1, 11.9977388381958),(2, 12.56324291229248),(3, 12.289109945297241),(4, 12.300051927566528),(5, 12.322132110595703),(6, 12.058021783828735),(7, 13.218597173690796),(8, 11.991199016571045),(9, 12.178853034973145),

When the monoproc : 当monoproc时:

   for n in range(nombre_de_cycles):
        debut = time.time()
        paris.reveil()
        duree = time.time() - debut
        print((n, duree), end=",")

gives

(0, 0.19302606582641602),(1, 0.030661821365356445),(2, 0.28160881996154785),(3, 0.04853320121765137),(4, 0.20609474182128906),(5, 0.04185295104980469),(6, 0.20528626441955566),(7, 0.040557146072387695),(8, 0.19860100746154785),(9, 0.11386394500732422),

Here are the functions : 以下是功能:

Class Ville:
    timestamp = 0
    def __init__(self, nb_de_cyclistes, Prestataire):
        self.timestamp =0

    def reveil(self):
        self.timestamp += 1
        list(map(lambda cycliste :cycliste.avancer(self.timestamp), self.cyclistes))

    def faire_avancer(cycliste):
        cycliste.avancer(Ville.timestamp)

    def reveil_multiproc(self, n_jobs=1):
        import multiprocessing
        self.timestamp += 1
        Ville.timestamp = self.timestamp
        pool            = multiprocessing.Pool(n_jobs)
        pool.map(Ville.faire_avancer, self.cyclistes)

What do I do wrong ? 我做错了什么?

Multiprocessing is not a 1-size-fits-all solution. 多重处理不是万能的解决方案。 You incur significant overhead with your solution to do very little work. 您的解决方案要做很少的工作,会产生大量开销。

You have the overhead of: Creating 2 worker processes, splitting self.cylistes into chunks, serializing it with pickle , and using IPC to send it to the subprocess. 您有以下开销:创建2个工作进程,将self.cylistes拆分为多个块,使用pickle对其进行序列化,然后使用IPC将其发送到子进程。 All this to simply call cycliste.avancer() which appears to do very little work. 所有这一切都可以简单地调用cycliste.avancer()来完成,而这似乎工作很少。

Having a large list of hundreds of thousands of items is trivial and means nothing when we can't see how much work you're doing in avancer() . 有成千上万个项目的大清单是微不足道的,当我们看不到您在avancer()正在做多少工作时,它avancer() The more items you have in here will actually most likely slow down the multiprocessing approach because it's unlikely you've implemented optimizations for pickle performance. 实际上,您拥有的更多物品很可能会减慢多处理方法的速度,因为您不太可能实现了pickle性能的优化。

You need to learn how to use Python's profiling tools (ex: cProfile, line_profiler) before making premature optimizations. 在进行过早的优化之前,您需要学习如何使用Python的分析工具(例如:cProfile,line_profiler)。

Use multiprocessing when you have long-running, CPU-intensive tasks that do not spend most of their time waiting on IO. 当您有长时间运行且占用大量CPU的任务而又没有花费大量时间等待IO时,请使用multiprocessing If each call to avancer() took 30 seconds to run, then you would see much better performance with multiprocessing then without it. 如果每次调用avancer()需要30秒才能运行,那么与没有它的情况相比,使用multiprocessing会看到更好的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM