2012-06-28 12 views
1

這裏是我的嘗試:如何傳遞一個類的方法deferred.defer的的PARAM在GAE

deferred.defer(class1().method1, class2.method2, arg) 
deferred.defer(class1().method1, class2.method2(), arg) 

這兩個失敗與錯誤:

Can't pickle <type 'instancemethod'>: it's not found as __builtin__.instancemethod 

在另一篇有關如何醃製實例方法,建議Steven Bethard的解決方案:http://bytes.com/topic/python/answers/552476-why-cant-you-pickle-instancemethods(朝向頁面底部)

該代碼已失去其格式,我一直無法成功使用該公司德解決我的延期問題。

+0

你不應該叫這個班。嘗試'class1.method1' – aschmid00

回答

5

功能在deferred.defer使用必須是一個,這直接導入的,全局函數。這是因爲Deferred處理函數幾乎肯定會在不同的解釋器實例中工作,所以必須通過它導入相關函數。

如果class1在你的代碼是指實際的類名,以解決這個問題最簡單的辦法是包裝調用它的全局函數裏面方法並將它傳遞給defer

def deferred_method_call(*args, **kwargs): 
    class1.method1(*args, **kwargs) 

deferred.defer(deferred_method_call, ...) 

在另一方面如果class1只是你點到實際的類變量的名字,你會希望把它作爲參數的函數:

def deferred_method_call(class_, *args, **kwargs): 
    class_.method1(*args, **kwargs) 

deferred.defer(deferred_method_call, class1, ...) 

這工作,因爲類對象(實例type)是可挑選的並且可以作爲參數傳遞給defer ed函數。

+0

如果我要使用我目前使用的類實例,該怎麼辦? 例如, ''' 類Foo(): 高清蝙蝠(): deferred.defer的(defer_function,個體經營) defer_function(類): class.method1() ''' 但這會得到一個不可取的實例 –

相關問題