pprocessから呼び出される関数を使用すると、Pythonでグローバル変数を変更できないようです。これが私の例です:
import pprocess
import time
numbers=[0,0,0,0,0,0,0,0,0,0]
# find system time and store in global variable
def find_time(index):
global numbers
x=time.time()
print "Setting element %s of numbers to %f" % (index, x)
numbers[index]=x
return x
# parallel execution of the function
results=pprocess.pmap(find_time, [0,1,2,3,4,5,6,7,8,9], limit=6)
for y in results:
print '%f' % y
# this list is unchanged
print numbers
# serial execution of the function
for x in [0,1,2,3,4,5,6,7,8,9]:
find_time(x)
# now it seems to work
print numbers
「数値」は単なるゼロのリストであり、デモンストレーションのために、各リスト要素を現在のシステム時刻に設定しようとしています。pprocessを使用して呼び出された場合、これは機能しませんが、単純なforループを使用して関数を呼び出すと、グローバル変数が変更されます。
私はグローバル変数について読むことに時間を費やしてきましたが、これが些細な問題ではないことを心から願っています。誰かが私に何が起こっているのか説明できますか?
どうもありがとう、
遠野