Re: self.channel.writeHeaders(version, code, reason, headers)\nbuiltins.AttributeError: \'NoneType\' object has no attribute \'writeHeaders\'\
Waqar Khan <[email protected]> Mon, 5 Aug 2019 14:26:03 -0400
| Newsgroups | gmane.comp.python.twisted |
|---|---|
| Message-ID | <CAJuJkHPnkc=dkT3PYgMw6HNzeSQ-c9ssfow=J7N64_UXMvts5w@mail.gmail.com> |
--===============6748359995808530535==
Content-Type: multipart/alternative; boundary="000000000000dcefab058f62d7a7"
--000000000000dcefab058f62d7a7
Content-Type: text/plain; charset="UTF-8"
Hi Jean,
Yeah, actually that is the example that I looked into and implemented my
code.
The issue I have is.. still some instances of interrupted responses are not
trapped.
Copying pasting my minimal implementation for your convinence
def print_json_response(resp, request):
request.write(json.dumps(resp))
request.close()
class FooResource(resource.Resource):
def render_GET(request):
future = asyncio.ensure_future(self.fetch_response(request))
// some async await functions
d = Deferred.fromFuture(future)
d.addCallback(print_json_response, request) // this is
actually where the error is triggered.
d.addErrback(lambda failure:
failure.trap(defer.CancelledError))
finished_errback = request.notifyFinish()
finished_errback.addErrback(self.handle_cancel, d) // simple
logs and cancels d by d.cancel()
return NOT_DONE_YET
async def fetch_response(self, request):
future = {}
try:
future = await some other async def.. which returns future
except asyncio.CancelledError as e:
print("Error..", e)
return future
def handle_cancel(failure, d):
d.cancel()
print(failure.getTraceback())
On Mon, Aug 5, 2019 at 2:21 PM Jean-Paul Calderone <
[email protected]> wrote:
> On Mon, Aug 5, 2019 at 2:15 PM Waqar Khan <[email protected]> wrote:
>
>> False alarm. Seems like there are some sneaky conditions when I get the
>> error message.
>> Like before, I use to get notifyFinish error everytime. Now, it seems
>> that 6/10 times things are "clean" but then 4/10 times there are
>> notifyFinish errors.
>> Wondering on your suggestion. How do I ensure whether the notifyFinish
>> error deferred has been fired or not.
>> Could I have like a vanilla "HelloWorld" example?
>> Thanks
>>
>>
> You can find an example of this here -
> https://twistedmatrix.com/documents/current/web/howto/web-in-60/interrupted.html
>
> Jean-Paul
>
>
>> On Mon, Aug 5, 2019 at 2:24 AM Waqar Khan <[email protected]> wrote:
>>
>>> Hi Glyph,
>>> I am not sure I understand.
>>>
>>> Is there a method/variable in request which keeps a track whether
>>> notifyFinish has been fired..
>>> So, I can do something like.
>>>
>>> if not request.hasFiredNotifyFinish:
>>> request.finish()
>>> ??
>>>
>>>
>>> I have sort of able to get around this issue.. though I can't put a
>>> finger on what actually worked.
>>> Here is what I did.. First change to 19.7rc01 version.. and just fix the
>>> channel issue.
>>>
>>> Next self.fetch_response(request).. This is an async def.. So what I
>>> did was..
>>>
>>> async def fetch_response(request):
>>> future = {}
>>> try:
>>> future = await some other async def.. which returns future
>>> except asyncio.CancelledError as e:
>>> print("Error..", e)
>>> return future
>>>
>>> I basically ended up doing this everywhere where there is async/await.
>>> Next, I added this:
>>> d.addErrback(lambda failure: failure.trap(asyncio.CancelledError))
>>>
>>>
>>> So.. now.. I don't see the notifyFinish error anymore. And I am bit
>>> terrified not to touch anything.. :-D
>>>
>>> But, I want to try out your suggestion as that seems like a more solid
>>> way to handle the issue. But I am not sure I quite understand, how do I
>>> figure out whether notifyFinish has been called before calling finish?
>>>
>>>
>>> On Mon, Aug 5, 2019 at 1:56 AM Glyph <[email protected]> wrote:
>>>
>>>>
>>>>
>>>> On Aug 4, 2019, at 9:04 PM, Waqar Khan <[email protected]> wrote:
>>>>
>>>> Ah yes.. That is true..
>>>>
>>>> If I comment out request.finish() (Here is the doc which I tried to
>>>> followed:
>>>> https://twistedmatrix.com/documents/13.0.0/web/howto/web-in-60/interrupted.html
>>>> )
>>>> Then actually.. when I try to test out the code... (via curl or like
>>>> doing requests.get .. to the URI)..
>>>> it is just stuck..
>>>>
>>>>
>>>> Yep! This makes sense; if you don't call `.finish()` *at all* then
>>>> your code will never tell the client that it's done.
>>>>
>>>> But if you call `.finish()` *after notifyFinish() has fired* then you
>>>> get that error.
>>>>
>>>> If you change your code to only call `.finish()` if the deferred
>>>> returned by notifyFinish() has not fired or failed yet, do you get the
>>>> error? It's possible that you still do, in which case, there's a bug in
>>>> Twisted that needs to be fixed.
>>>>
>>>> I do want to convey my thanks for the help. Really appreciate it.
>>>>
>>>>
>>>> Thanks for using Twisted :)
>>>> -g
>>>>
>>>> _______________________________________________
>>>> Twisted-Python mailing list
>>>> [email protected]
>>>> https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>>>>
>>> _______________________________________________
>> Twisted-Python mailing list
>> [email protected]
>> https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>>
> _______________________________________________
> Twisted-Python mailing list
> [email protected]
> https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
--000000000000dcefab058f62d7a7
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hi Jean,<div>=C2=A0 Yeah, actually that is the example tha=
t I looked into and implemented my code.</div><div><br>The issue I have is.=
. still some instances of interrupted responses are not trapped.=C2=A0</div=
><div>Copying pasting my minimal implementation for your convinence</div><d=
iv><br></div><div><div>def=C2=A0print_json_response(resp, request):</div><d=
iv>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0request.write(json.dumps(resp))</div><=
div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0request.close()</div></div><div><div>=
class FooResource(resource.Resource):</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0=
def render_GET(request):</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 future =3D asyncio.ensure_future(self.fetch_response(request)) /=
/ some async await functions</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 d =3D=C2=A0Deferred.fromFuture(future)</div><div>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0d.addCallback(print_json_respon=
se, request) // this is actually where the error is triggered.</div><div>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0d.addErrback(lambda f=
ailure: failure.trap(defer.CancelledError))</div><div>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0finished_errback =3D request.notifyFinish(=
)</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 finished_errba=
ck.addErrback(self.handle_cancel, d) // simple logs and cancels d by d.canc=
el()</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return NOT_=
DONE_YET</div></div><div><br></div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0async de=
f fetch_response(self, request):</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 futu=
re =3D {}</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 try:</div><div>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0future =3D await some other async def.. whic=
h returns future</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 except asyncio.Cance=
lledError as e:</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print(&q=
uot;Error..", e)</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return future</=
div><div>=C2=A0 =C2=A0 def handle_cancel(failure, d):</div><div>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 d.cancel()</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 print(failure.getTraceback())</div><div><br></div><div><br></div><di=
v><br></div><div><br></div></div><br><div class=3D"gmail_quote"><div dir=3D=
"ltr" class=3D"gmail_attr">On Mon, Aug 5, 2019 at 2:21 PM Jean-Paul Caldero=
ne <<a href=3D"mailto:[email protected]">[email protected]=
om</a>> wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margi=
n:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex=
"><div dir=3D"ltr"><div dir=3D"ltr">On Mon, Aug 5, 2019 at 2:15 PM Waqar Kh=
an <<a href=3D"mailto:[email protected]" target=3D"_blank">wk80333@gmail=
.com</a>> wrote:<br></div><div class=3D"gmail_quote"><blockquote class=
=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rg=
b(204,204,204);padding-left:1ex"><div dir=3D"ltr">False alarm. Seems like t=
here are some sneaky conditions when I get the error message.<div>Like befo=
re, I use to get notifyFinish error everytime. Now, it seems that 6/10 time=
s=C2=A0 things are "clean" but then 4/10 times there are notifyFi=
nish errors.</div><div>Wondering on your suggestion. How do I ensure whethe=
r the notifyFinish error deferred has been fired or not.</div><div>Could I =
have like a vanilla "HelloWorld" example?</div><div>Thanks</div><=
/div><br></blockquote><div><br></div><div>You can find an example of this h=
ere -=C2=A0<a href=3D"https://twistedmatrix.com/documents/current/web/howto=
/web-in-60/interrupted.html" target=3D"_blank">https://twistedmatrix.com/do=
cuments/current/web/howto/web-in-60/interrupted.html</a></div><div><br></di=
v><div>Jean-Paul</div><div>=C2=A0</div><blockquote class=3D"gmail_quote" st=
yle=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padd=
ing-left:1ex"><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_at=
tr">On Mon, Aug 5, 2019 at 2:24 AM Waqar Khan <<a href=3D"mailto:wk80333=
@gmail.com" target=3D"_blank">[email protected]</a>> wrote:<br></div><bl=
ockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-lef=
t:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr">Hi Glyph,<d=
iv>=C2=A0 =C2=A0 I am not sure I understand.=C2=A0</div><div><br></div><div=
>Is there a method/variable in request which keeps a track whether notifyFi=
nish has been fired..</div><div>So, I can do something like.</div><div><br>=
</div><div>if not request.hasFiredNotifyFinish:</div><div>=C2=A0 =C2=A0 =C2=
=A0 =C2=A0request.finish()</div><div>??</div><div><br></div><div><br></div>=
<div>I have sort of able to get around this issue.. though I can't put =
a finger on what actually worked.=C2=A0</div><div>Here is what I did.. Firs=
t change to 19.7rc01 version.. and just fix the channel issue.</div><div><b=
r></div><div>Next=C2=A0 self.fetch_response(request).. This is an async def=
.. So what I did was..</div><div><br></div><div>async def fetch_response(re=
quest):</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 future =3D {}</div><div>=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 try:</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0future =3D await some other async def.. which returns future</div><di=
v>=C2=A0 =C2=A0 =C2=A0 =C2=A0 except asyncio.CancelledError as e:</div><div=
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print("Error..", e)</di=
v><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return future</div><div><br></div><div>I=
basically ended up doing this everywhere where there is async/await.</div>=
<div>Next, I added this:</div><div>d.addErrback(lambda failure: failure.tra=
p(asyncio.CancelledError))=C2=A0</div><div><br></div><div><br></div><div>So=
.. now.. I don't see the notifyFinish error anymore. And I am bit terri=
fied not to touch anything.. :-D</div><div><br></div><div>But, I want to tr=
y out your suggestion as that seems like a more solid way to handle the iss=
ue. But I am not sure I quite understand, how do I figure out whether notif=
yFinish has been called before calling finish?=C2=A0=C2=A0</div><div><br></=
div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_at=
tr">On Mon, Aug 5, 2019 at 1:56 AM Glyph <<a href=3D"mailto:glyph@twiste=
dmatrix.com" target=3D"_blank">[email protected]</a>> wrote:<br></=
div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bor=
der-left:1px solid rgb(204,204,204);padding-left:1ex"><div><br><div><br><bl=
ockquote type=3D"cite"><div>On Aug 4, 2019, at 9:04 PM, Waqar Khan <<a h=
ref=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>>=
; wrote:</div><br class=3D"gmail-m_-3580166139866134158gmail-m_735877766536=
14432gmail-m_1609281543949452197gmail-m_966327303200188702Apple-interchange=
-newline"><div><div dir=3D"ltr">Ah yes.. That is true..<div><br></div><div>=
If I comment out request.finish() (Here is the doc which I tried to followe=
d:=C2=A0<a href=3D"https://twistedmatrix.com/documents/13.0.0/web/howto/web=
-in-60/interrupted.html" target=3D"_blank">https://twistedmatrix.com/docume=
nts/13.0.0/web/howto/web-in-60/interrupted.html</a>)=C2=A0</div><div>Then a=
ctually.. when I try to test out the code... (via curl or like doing reques=
ts.get .. to the URI)..=C2=A0</div><div>it is just stuck..</div></div></div=
></blockquote><div><br></div><div>Yep!=C2=A0 This makes sense; if you don&#=
39;t call `.finish()` <i>at all</i><span style=3D"font-style:normal">=C2=A0=
then your code will never tell the client that it's done.</span></div><=
div><span style=3D"font-style:normal"><br></span></div><div>But if you call=
`.finish()` <i>after notifyFinish() has fired</i><span style=3D"font-style=
:normal">=C2=A0then you get that error.</span></div><div><span style=3D"fon=
t-style:normal"><br></span></div><div><span style=3D"font-style:normal">If =
you change your code to only call `.finish()` if the deferred returned by n=
otifyFinish() has not fired or failed yet, do you get the error?=C2=A0 It&#=
39;s possible that you still do, in which case, there's a bug in Twiste=
d that needs to be fixed.</span></div><br><blockquote type=3D"cite"><div><d=
iv dir=3D"ltr"><div>I do want to convey my thanks for the help. Really appr=
eciate it.</div></div></div></blockquote><br></div><div>Thanks for using Tw=
isted :)</div><div>-g</div><div><br></div></div>___________________________=
____________________<br>
Twisted-Python mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">Twist=
[email protected]</a><br>
<a href=3D"https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-pytho=
n" rel=3D"noreferrer" target=3D"_blank">https://twistedmatrix.com/cgi-bin/m=
ailman/listinfo/twisted-python</a><br>
</blockquote></div>
</blockquote></div>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">Twist=
[email protected]</a><br>
<a href=3D"https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-pytho=
n" rel=3D"noreferrer" target=3D"_blank">https://twistedmatrix.com/cgi-bin/m=
ailman/listinfo/twisted-python</a><br>
</blockquote></div></div>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">Twist=
[email protected]</a><br>
<a href=3D"https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-pytho=
n" rel=3D"noreferrer" target=3D"_blank">https://twistedmatrix.com/cgi-bin/m=
ailman/listinfo/twisted-python</a><br>
</blockquote></div>
--000000000000dcefab058f62d7a7--
--===============6748359995808530535==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline
X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KVHdpc3RlZC1Q
eXRob24gbWFpbGluZyBsaXN0ClR3aXN0ZWQtUHl0aG9uQHR3aXN0ZWRtYXRyaXguY29tCmh0dHBz
Oi8vdHdpc3RlZG1hdHJpeC5jb20vY2dpLWJpbi9tYWlsbWFuL2xpc3RpbmZvL3R3aXN0ZWQtcHl0
aG9uCg==
--===============6748359995808530535==--