简体   繁体   English

python中嵌入式循环的多线程

[英]Multi-threading for embedded loops in python

I have a python code which does some manipulation of data contained in a Shapefile. 我有一个python代码,可以对Shapefile中包含的数据进行一些操作。

Among others stuffs, the code does this: 在其他东西中,代码执行以下操作:

 xxx=0
 for i in channels:
      ptsi=mat(shapes[i].points)
      xx = ptsi[:,0]
      yy = ptsi[:,1]

      nanx=argwhere(isnan(xx))      
      nany=argwhere(isnan(yy))

      if (nanx == nany and len(nanx) != 0):
          xx[nanx] = []
          yy[nany] = []
      elif (len(nanx) != 0 or len(nany) != 0):
          xx = []
          yy = []

      if (len(xx) > 0):          
       yyy = 0 
       dd = str(i*100/N_channels) + '%\r\r'
      # os.write(1,dd)
       dist = zeros(len(xx)-1)

       dist = dist_min
       for j in channels:
           pts=mat(shapes[j].points)
           xx2 = pts[:,0]
           yy2 = pts[:,1]

           nanx=argwhere(isnan(xx2))          
           nany=argwhere(isnan(yy2))

           if (nanx == nany and len(nanx) != 0):
            xx2[nanx] = []
            yy2[nany] = []
           elif (len(nanx) != 0 or len(nany) != 0):
            xx2 = []
            yy2 = []
           if (len(xx2) > 0):  
            if (i != j): 
               ds = gaa(xx,yy,xx2[0],yy2[0])
               de = gaa(xx,yy,xx2[-1],yy2[-1])
               nande = ~isnan(de)
               nands = ~isnan(ds)
               fe = np.argwhere(de<=dist)
               fs = np.argwhere(ds<=dist)
               nozeroe = array(where(de != 0))
               nozeroe = nozeroe[0]
               nozeros = array(where(ds != 0))
               nozeros = nozeros[0] 
               if(fs.size >0):
                    iis=array(np.argwhere(array(ds==min(ds))))

                    iis = iis.flatten()
                    iis = iis[0]
                    p1 = xxx + iis 
                    p2 = yyy
                    G.add_edge(p2,p1,weight=w_con)

               elif (fe.size > 0):
                    iie=array(np.argwhere(array(de==min(de))))
                    iie = iie.flatten()
                    iie = iie[0]
                    p1 = xxx + iie
                    p2 = yyy  + len(pts) -1
                    G.add_edge(p2,p1,weight=w_con)

           yyy = yyy + len(pts)

      xxx = xxx + len(ptsi)

Basically, the code scans over polylines and search for a minimal distance in order to merge then in a common graph (using Networkx). 基本上,代码会扫描折线并搜索最小距离,然后合并到一个普通图形中(使用Networkx)。 This part works fine except that this is really slow as I deal with more than 100's of thousand of objects (It takes roughly 20 hours in its current version). 这部分工作正常,除了它确实很慢之外,因为我处理的对象超过了十万个(在当前版本中大约需要20个小时)。

These embedded loops are not efficient, so I'd like to know, if using multithreading could be helpful and if so, how can I modify this very part of the code? 这些嵌入式循环效率不高,所以我想知道,使用多线程是否有帮助,如果可以,那么如何修改这部分代码? I'm fine with CUDA or OpenCL if it can help. 如果可以,我对CUDA或OpenCL表示满意。

Thanks for any feedback! 感谢您的任何反馈!

由于使用全局解释器锁 ,Python代码无法通过多线程完全利用多个内核,并且需要使用多重处理才能充分利用多个内核。

More threads will not help; 更多线程将无济于事; more processes might, but if you can use CUDA, that would probably be a good move. 可能会有更多的过程,但是如果您可以使用CUDA,那将是一个不错的选择。 A thread is just a way to allow multiple things to share a processes CPU time, it does not speed up slow code, but will just slow you down more. 线程只是一种允许多个事物共享一个进程的CPU时间的方法,它不会加快慢速的代码,但是只会使您的速度降低更多。

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

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