Twisted.web的isLeaf

在使用Twisted.web模塊搭建web服務(wù)時,發(fā)現(xiàn)要定義isLeaf變量。 很奇怪。這個變
量出現(xiàn)的莫名其妙,所以就找了相關(guān)的資料看。也有了一些理解。

引用Stack Overflow上的回答:

See twisted.web.resource.IResource.isLeaf documentation --

Signal if this IResource implementor is a "leaf node" or not. If True, getChildWithDefault will not be called on this Resource.

The way Twisted finds a resource to render is by splitting the path into segments, and calling "getChildWithDefault" on the root, and then whatever the root returns and so on. It stops if it either runs out of segments, or a "leaf" (i.e., isLeaf=True) resource is found.

At that point, it will call the render method on the resource. In a leaf resource, the renderer will often want to look at the "request.postpath" attribute -- stashed there is the list of segments that have not been used up to find the resource.

In general, only resources that are leaves are rendered; this can be because isLeaf is set to True or because when traversing the resource hierarchy, that resource is where we are when the URL runs out. However, when isLeaf is True for a resource, its getChild method is never called. Thus, for resources that have children, isLeaf cannot be set to True.

參考資料:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容