[英]Difficulty using stackless python, cannot write to a dict
我有一個簡單的map-reduce類型算法,我想在python中實現並利用多個內核。
我在某處讀到,使用2.6中的本機線程模塊的線程不使用多個內核。 真的嗎?
我什至使用無堆棧python實現了它,但是我陷入了奇怪的錯誤[更新:快速搜索顯示,堆棧較少,不允許多個內核,那么它們還有其他替代方法嗎?]
def Propagate(start,end):
print "running Thread with range: ",start,end
def maxVote(nLabels):
count = {}
maxList = []
maxCount = 0
for nLabel in nLabels:
if nLabel in count:
count[nLabel] += 1
else:
count[nLabel] = 1
#Check if the count is max
if count[nLabel] > maxCount:
maxCount = count[nLabel];
maxList = [nLabel,]
elif count[nLabel]==maxCount:
maxList.append(nLabel)
return random.choice(maxList)
for num in range(start,end):
node=MapList[num]
nLabels = [Label[k] for k in Adj[node]]
if (nLabels!=[]):
Label[node] = maxVote(nLabels)
else:
Label[node]=node
但是,在上面的代碼中,分配給Label的值(即字典中的更改)丟失了。
上面的傳播函數用作可調用的微線程(即TaskLets)
使用標准庫的多處理模塊-它模仿threading
模塊的接口(以簡化現有多線程代碼的移植),並可以充分利用您擁有的所有內核。 線程和無堆棧都是單核(實際上,無堆棧是非搶占式的,因此針對它的編程與針對線程或多處理的編程完全不同)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.