<div dir="ltr"><div><div><div><div><div><div>Hi , <br><br></div>   I had a Question,i hope i'll find the solution here.<br><br>Say i have a Queue.<br> >>> h = Queue.Queue(maxsize=0)<br>>>> h.put(1)<br>>>> h.put(2)<br>>>> h.empty()<br>False<br>>>> h.join()<br>>>> h.empty()<br>False<br>>>> h.get()<br>1<br>>>> h.get()<br>2<br>>>> h.get()<br></div>Blocked.......................<br><br></div>My Question is : <br>In a single threaded environment why does the get() gets blocked , instead of raising an exception.On interpreter i have no way to resume working.<br></div><br>And my second question is :<br>Why doe we have to explicitly call task_done after get(). why  doesn't get() implicitly call task_done().<br></div>as for put() entry for unfinished_task is automatically added .why not get deleted in get() then.<br><br></div>Thanks.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, May 29, 2015 at 10:16 AM, Ben Leslie <span dir="ltr"><<a href="mailto:benno@benno.id.au" target="_blank">benno@benno.id.au</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<br>
Apologies in advance; I'm not a regular, and this may have been<br>
handled already (but I couldn't find it when searching).<br>
<br>
I've been using the new async/await functionality (congrats again to<br>
Yury on getting that through!), and I'd like to get a stack trace<br>
between the place at which blocking occurs and the outer co-routine.<br>
<br>
For example, consider this code:<br>
<br>
"""<br>
async def a():<br>
    await b()<br>
<br>
async def b():<br>
    await switch()<br>
<br>
@types.coroutine<br>
def switch():<br>
    yield<br>
<br>
coro_a = a()<br>
coro_a.send(None)<br>
"""<br>
<br>
At this point I'd really like to be able to somehow get a stack trace<br>
similar to:<br>
<br>
test.py:2<br>
test.py:4<br>
test.py:9<br>
<br>
Using the gi_frame attribute of coro_a, I can get the line number of<br>
the outer frame (e.g.: line 2), but from there there is no way to<br>
descend the stack to reach the actual yield point.<br>
<br>
I thought that perhaps the switch() co-routine could yield the frame<br>
object returned from inspect.currentframe(), however once that<br>
function yields that frame object has f_back changed to None.<br>
<br>
A hypothetical approach would be to work the way down form the<br>
outer-frame, but that requires getting access to the co-routine object<br>
that the outer-frame is currently await-ing. Some hypothetical code<br>
could be:<br>
<br>
"""<br>
def show(coro):<br>
    print("{}:{}".format(coro.gi_frame.f_code.co_filename,<br>
coro.gi_frame.f_lineno))<br>
    if dis.opname[coro.gi_code.co_code[coro.gi_frame.f_lasti + 1]] ==<br>
'YIELD_FROM':<br>
        show(coro.gi_frame.f_stack[0])<br>
"""<br>
<br>
This relies on the fact that an await-ing co-routine will be executing<br>
a YIELD_FROM instruction. The above code uses a completely<br>
hypothetical 'f_stack' property of frame objects to pull the<br>
co-routine object which a co-routine is currently await-ing from the<br>
stack. I've implemented a proof-of-concept f_stack property in the<br>
frameobject.c just to test out the above code, and it seems to work.<br>
<br>
With all that, some questions:<br>
<br>
1) Does anyone else see value in trying to get the stack-trace down to<br>
the actual yield point?<br>
2) Is there a different way of doing it that doesn't require changes<br>
to Python internals?<br>
3) Assuming no to #2 is there a better way of getting the information<br>
compared to the pretty hacking byte-code/stack inspection?<br>
<br>
Thanks,<br>
<br>
Ben<br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/jaivishkothari10104733%40gmail.com" rel="noreferrer" target="_blank">https://mail.python.org/mailman/options/python-dev/jaivishkothari10104733%40gmail.com</a><br>
</blockquote></div><br></div>