I use Boto to access Amazon S3. And to download files, I can assign a callback function. The problem is that I cannot access the necessary variables from this callback function until I make them global. In the other hand, if I make them global, they are also global for all other Celery tasks (until I restart Celery), since the file is downloaded from the Celery task.
Here is a function that downloads a JSON file with information on the progress of the video conversion.
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")
And here there are 2 callback functions for loading frames generated by ffmpeg during video conversion, and a JSON file with progress information.
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
Theoretically, I can pass the uploading_frame variable to the upload_json function, but it will not receive json_upload_callback since Boto executed it.In fact, I could write something like this.
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
Which, however, does not allow editing a value from a function, just allows you to read the value.
def myfunc():
stuff = 17
def lfun(arg):
print "got arg", arg, "and stuff is", stuff
return lfun
my_function = myfunc()
my_function("hello")
It 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")
And this gives the UnboundLocalError reference: the local variable 'stuff', indicated before the assignment.
Thank.