簡體   English   中英

Python多處理速度問題

[英]Python Multiprocessing speed issue

我有一個嵌套for形式循環

while x<lat2[0]:
    while y>lat3[1]:
        if (is_inside_nepal([x,y])):
            print("inside")
        else:
            print("not")
        y = y - (1/150.0)
    y = lat2[1]
    x = x + (1/150.0)
#here lat2[0] represents a large number

現在執行通常需要50秒鍾左右的時間 而且我已將此循環更改為多處理代碼。

def v1find_coordinates(q):
  while not(q.empty()):

    x1 = q.get()
    x2 = x1 + incfactor
    while x1<x2:
        def func(x1): 
            while y>lat3[1]:
                if (is_inside([x1,y])):
                    print x1,y,"inside"
                else:
                    print x1,y,"not inside"
                y = y - (1/150.0)

        func(x1)
        y = lat2[1]
        x1 = x1 + (1/150.0)

incfactor = 0.7
xvalues = drange(x,lat2[0],incfactor)
#this drange function is to get list with increment factor as decimal
cores = mp.cpu_count()
q = Queue()
for i in xvalues:
    q.put(i)
for i in range(0,cores):
    p = Process(target = v1find_coordinates,args=(q,) )
    p.start()
    p.Daemon = True
    processes.append(p) 
for i in processes:
    print ("now joining")
    i.join()   

這種多處理代碼還需要大約50s的執行時間 這意味着兩者之間沒有時間差異。

我也嘗試過使用游泳池。 我還管理了塊的大小。 我已經搜索並搜索了其他stackoverflow。 但是找不到滿意的答案。

我唯一能找到的答案是在過程管理上花時間使兩個結果相同 如果這是原因,那么我如何進行多處理工作以獲得更快的結果?

從Python用C實施會帶來更快的結果嗎?

我並不期望獲得驚人的結果,但是從常識上可以看出,在4個內核上運行應該比在1個內核上運行快得多。 但是我得到了類似的結果。 任何幫助將不勝感激。

您似乎正在使用線程Queue(來自Queue import Queue)。 由於Process使用fork(),因此無法正常工作,並且將整個Queue克隆到每個輔助進程中

采用:

from multiprocessing import Queue

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM