Boto를 사용하여 Amazon S3에 액세스합니다. 파일 업로드의 경우 콜백 함수를 할당 할 수 있습니다. 문제는 글로벌 변수가 될 때까지 해당 콜백 함수에서 필요한 변수에 액세스 할 수 없다는 것입니다. 다른 한편으로, 내가 글로벌하게 만들면 파일 업로드가 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
이론적으로, 나는 upload_json 함수에 uploading_frame 변수를 전달할 수 있지만,이 BOTO에 의해 실행됩니다으로 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")
이것은 작동합니다.
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