python 多線程疏鬆陣列乘法

來源:互聯網
上載者:User

標籤:release   item   ima   多線程   com   adl   array   value   lease   

 1 import threading, time 2 import numpy as np 3 res = [] 4 class MyThread(threading.Thread): 5     def __init__(self,i,j,m1,m2): 6         threading.Thread.__init__(self) 7         self.x, self.y = i,j 8         self.m1, self.m2 = m1, m2 9     def run(self):10         global res, lock11         if lock.acquire():12             m1 = self.m1[self.m1[:,0]==self.x]13             m2 = self.m2[self.m2[:,1]==self.y]14             value = 0.15             for item1 in m1:16                 for item2 in m2:17                     if item1[1] == item2[0]:18                         value += item1[2]*item2[2]19             res.append([self.x,self.y,value])20             lock.release()21 if "__main__" == __name__:22     m1 = [[2,2],[0,0,1],[0,1,2],[1,0,3],[1,1,4]]23     m2 = [[2,3],[0,0,2],[0,2,1],[1,2,3],[1,1,4]]24     s1, s2 = m1[0], m2[0]25     assert s1[1]==s2[0], ‘mismatch‘26     m1_value = np.array(m1[1:])27     m2_value = np.array(m2[1:])  28     rows, cols = s1[0], s2[1]29     res.append([rows, cols])30     ThreadList = []31     lock = threading.Lock()32     for i in range(rows):33         for j in range(cols):34             t = MyThread(i,j,m1_value,m2_value)35             ThreadList.append(t)36     for t in ThreadList:37         t.start()38     for t in ThreadList:39         t.join()40     print (res)

 

python 多線程疏鬆陣列乘法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.