내가 여기에 참조로 Twisted's 구현을 사용하고 비동기 코드를 만드는 inlineCallbacks
동기 코드가 작동처럼 보이는 방법을 이해하려고왜 트위스트의 inlineCallbacks는 '이연'개체를 반환하지
정상 기능을 :.. 입력 → 출력
inlineCallbacks
장식 발전기 기능 : 이연 → 입력
그것은 Deferred
즉 callback
에 등록 할 수있는 객체
from twisted.internet import defer
intermediate_deferred = None
@defer.inlineCallbacks
def example():
global intermediate_deferred
print("Started")
intermediate_deferred = defer.Deferred()
result = yield intermediate_deferred
print("Resuming for the first time with %s" % result)
intermediate_deferred = defer.Deferred()
result = yield intermediate_deferred
print("Resuming for the second time with %s" % result)
generator_deferred = example()
intermediate_deferred.callback("One")
intermediate_deferred.callback("Two")
- (가)를 소비 즉 지연된 객체를 반환하는 진정한 필요가 무엇입니까?
- 누가 최종 값으로 호출합니까?
- 반환되지 않으면 어떻게됩니까?
repl
에서 나는 왜이 반환 값이 필요한지 알지 못합니다. 나의 근본적인 문제는 내가이 기능의 소비자에 관해서 아직도 생각하고 있지 않다는 사실로부터 생겨난 것 같다.
그래서 일부 웹 프레임 워크를 가지고 코드가 될 방법을 가정 해 봅시다 :
# Assume it returns 'example' function object.
function_to_call = get_routing_function(request)
d = function_to_call()
# But who takes care of calling it back? The Event Loop has to do that?
# So the Event Loop has one important job of monitoring and calling back
# such Deferred's to complete the full cycle?
d.addCallback(handle_http_response)
@defer.inlineCallbacks
def get_user_details(user_idn):
result = yield get_user_details_from_cache(user_idn)
if not result:
result = yield get_user_details_from_db(user_idn)
result = post_process_user_details(user_idn)
return result
누구나 소프트웨어 엔지니어링에 더 적합하다고 생각하는 사람이 있으면 언제든지 알려 주시기 바랍니다. 거기에 주제가있는 한 그것을 마이그레이션 할 것입니다. – Nishant