나는 twisted.python.context
을 사용하려고 시도하지만 처음에는 컨텍스트가 사라집니다. deferToThread
.꼬인 상태에서 컨텍스트 사용
from twisted.internet import reactor, defer, threads
from twisted.python import context
def _print_context(msg):
cont = context.get('cont')
print "{msg}: {context}".format(msg=msg, context=cont)
def sub_call():
_print_context("In sub_call")
@defer.inlineCallbacks
def with_context():
_print_context("before thread")
yield threads.deferToThread(sub_call)
_print_context("after thread")
reactor.stop()
def test():
cont = {'cont': "TestContext"}
context.call(cont, with_context)
reactor.callLater(0, test)
reactor.run()
나는 deferToThread
전 sub_call
에 컨텍스트가 없지만, deferToThread
이후에는 상황.
deferToThread
뒤에 컨텍스트가있는 방법이 있습니까?
고마워요. 물론 수업을 사용합니다. 이것은 단지 문제의 최소한의 예일뿐입니다. 그러나 내가 볼 수있는 유일한 해결책은 메서드 params에서 컨텍스트를 전달하는 것입니다. 나는이 해결책을 좋아하지 않지만 다른 어떤 것도 찾을 수 없다. – aborilov
왜 나는 입력 값을 매개 변수로 메서드에 전달하는 솔루션을 좋아하지 않는지 궁금합니다. – Glyph
@Glyph, 그것은 선택적 매개 변수이므로 각 메서드에 나타납니다.이 메서드의 대부분은이 매개 변수가 필요하지 않으며 심지어 존재하지도 않아도 다음 메서드로 전달해야합니다 . 이 매개 변수에서는 시스템에서 작업을 수행하는 사용자에 대한 정보를 전달합니다. 그리고'컨텍스트', 나는이 문제를 해결하는 최선의 해결책이라고 생각한다. – aborilov