我使用Boto訪問Amazon S3。而對於文件上傳,我可以分配一個回調函數。問題是,我無法從該回調函數訪問所需的變量,直到我使它們成爲全局變量。另一方面,如果我使它們成爲全局的,它們對於所有其他Celery任務也是全局的(直到我重新啓動Celery),因爲文件上傳是從Celery任務執行的。如何從回調函數訪問(和編輯)變量?
這是一個函數,可以上傳帶有視頻轉換進度信息的JSON文件。
def upload_json():
global current_frame
global path_to_progress_file
global bucket
json_file = Key(bucket)
json_file.key = path_to_progress_file
json_file.set_contents_from_string('{"progress": "%s"}' % current_frame,
cb=json_upload_callback, num_cb=2, policy="public-read")
這裏是用於上載的視頻轉換和與所述進度信息的JSON文件期間由ffmpeg的生成的幀2個回調函數。
# Callback functions that are called by get_contents_to_filename.
# The first argument is representing the number of bytes that have
# been successfully transmitted from S3 and the second is representing
# the total number of bytes that need to be transmitted.
def frame_upload_callback(transmitted, to_transmit):
if transmitted == to_transmit:
upload_json()
def json_upload_callback(transmitted, to_transmit):
global uploading_frame
if transmitted == to_transmit:
print "Frame uploading finished"
uploading_frame = False
從理論上講,我可以通過uploading_frame變量的upload_json功能,但它不會得到,因爲它是由寶途執行json_upload_callback。
事實上,我可以寫這樣的東西。
In [1]: def make_function(message):
...: def function():
...: print message
...: return function
...:
In [2]: hello_function = make_function("hello")
In [3]: hello_function
Out[3]: <function function at 0x19f4c08>
In [4]: hello_function()
hello
但是,哪個不允許您編輯函數中的值,只是讓您讀取值。
def myfunc():
stuff = 17
def lfun(arg):
print "got arg", arg, "and stuff is", stuff
return lfun
my_function = myfunc()
my_function("hello")
This Works。
def myfunc():
stuff = 17
def lfun(arg):
print "got arg", arg, "and stuff is", stuff
stuff += 1
return lfun
my_function = myfunc()
my_function("hello")
而且這給了一個UnboundLocalError:在分配之前引用的局部變量'stuff'。
謝謝。
您的例子並不表明uploading_frame是如何使用的,所以很難理解爲什麼你的事件需要它。創建一個更簡單的例子,如果沒有任何更清楚地顯示問題的S3相關內容,也可能會有所幫助。 – dkagedal 2011-01-27 10:48:46