2011-01-27 62 views
5

我使用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'。

谢谢。

+0

您的例子并不表明uploading_frame是如何使用的,所以很难理解为什么你的事件需要它。创建一个更简单的例子,如果没有任何更清楚地显示问题的S3相关内容,也可能会有所帮助。 – dkagedal 2011-01-27 10:48:46

回答

12

在Python 2.x中,闭包是只读的。但是,您可以使用一个封闭在一个可变值...即

def myfunc(): 
    stuff = [17] # <<---- this is a mutable object 
    def lfun(arg): 
     print "got arg", arg, "and stuff[0] is", stuff[0] 
     stuff[0] += 1 
    return lfun 

my_function = myfunc() 
my_function("hello") 
my_function("hello") 

如果您是不是使用Python 3.x中nonlocal可用于指定,在封闭的读/写使用的变量是关键词不是本地的,但应该从封闭范围被捕获:

def myfunc(): 
    stuff = 17 
    def lfun(arg): 
     nonlocal stuff 
     print "got arg", arg, "and stuff is", stuff 
     stuff += 1 
    return lfun 

my_function = myfunc() 
my_function("hello") 
my_function("hello") 
0

一个简单的方法做这些事情是使用本地函数

def myfunc(): 
    stuff = 17 
    def lfun(arg): 
     print "got arg", arg, "and stuff is", stuff 
     stuff += 1 
    def register_callback(lfun) 

这将创建一个新的功能,每次调用myfunc的时间,这将能够使用本地的“东西”的副本。

+0

请参阅上面的更新。 – aruseni 2011-01-27 11:15:41

+0

谢谢你的回答。 – aruseni 2011-01-27 11:45:32

+1

[This does not work。](http://codepad.org/p3N4gXLC) – 2011-01-27 12:18:52

3

您可以通过functools.partial创建部分功能。这是一种调用函数,其中包含一些预先写入调用的变量。但是,为了完成这项工作,您需要将一个可变值(例如列表或字典)传递给该函数,而不仅仅是一个布尔值。

from functools import partial 
def callback(arg1, arg2, arg3): 
    arg1[:] = [False] 
    print arg1, arg2, arg3 

local_var = [True] 
partial_func = partial(callback, local_var) 

partial_func(2, 1) 
print local_var # prints [False]