我在包中廣泛使用twisted.internet.defer
,我遇到了一個問題,我花了2天后無法解決。以下是我的問題場景。Python扭曲的延遲returnValue與字典不兼容
# all imports done and correct
class infrastructure: # line 1
@inlineCallbacks
def dict_service(self):
client = MyClient()
services = yield client.listServices() # line 5
ret = (dict(service.name, [cont.container_id for cont in service.instances]) for service in dockerServices)
returnValue(ret) # line 7
我打電話給我的客戶,它返回我的服務列表。返回類型爲twisted.internet.defer.ReturnValue
。
class myinterface:
# has infrastructure
def init:
data = dict(
container_services=self._infrastructure.dict_service(),
)
當執行這個我得到以下錯誤,我無法理解。有人可以請幫忙。
raise TypeError(repr(o) + \" is not JSON serializable\")\nexceptions.TypeError: <DeferredWithContext at 0x4bfbb48 current result: <twisted.python.failure.Failure <type 'exceptions.NameError'>>> is not JSON serializable\n"
是不是因爲那個包裹dict
與returnValue
創設問題?
檢查和將更新它。 – chaosguru
我沒有明確任何json.dumps。但總的堆棧跟蹤看起來像這樣。 – chaosguru
你的程序中的某些東西必須用JSON來做_something_。扭曲本身當然不是試圖讓JSON序列化你從inlineCallbacks-decorated函數中產生的任何值。 –