Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103296

Re: how to get the list form dictionary's values

From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: how to get the list form dictionary's values
Date 2016-02-21 17:16 +0200
Organization A noiseless patient Spider
Message-ID <87vb5inlfu.fsf@elektro.pacujo.net> (permalink)
References <554c09f2-6a6b-4576-9c9d-50d6234c8583@googlegroups.com> <mailman.6.1456063449.20994.python-list@python.org> <813ad31d-b208-496f-91b0-5f39aef28965@googlegroups.com> <mailman.8.1456066906.20994.python-list@python.org>

Show all headers | View raw


Chris Angelico <rosuav@gmail.com>:

> On Mon, Feb 22, 2016 at 1:15 AM, Ho Yeung Lee <davidbenny2000@gmail.com> wrote:
>> 0 ---> 2 --> 3--> 1 ---> 0
>>         ---> 4 /
>
> So after task 2 completes, tasks 3 and 4 both become available (and
> can run in parallel), but then task 1 must wait until both have
> finished before it runs? I'm not an asyncio expert, but this sounds
> like a fairly straight-forward dependency requirement. If you were to
> implement these as four separate threads, you would have threads 3 and
> 4 raise semaphores which thread 1 blocks on; I'm sure there'll be an
> equivalent in asyncio.
>
> [...]
>
> (Can an asyncio expert tell me how to tidy this code up, please? I'm
> sure this isn't the cleanest.)

Not an expert, but this does the trick:

========================================================================
#!/usr/bin/env python3

import asyncio

def main():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(t0())
    
async def t0():
    print("start t0")
    await t2()
    await asyncio.wait([ t3(), t4() ])
    await t1()
    print("finish t0")

async def t1():
    print("start t1")
    await asyncio.sleep(1)
    print("finish t1")

async def t2():
    print("start t2")
    await asyncio.sleep(1)
    print("finish t2")

async def t3():
    print("start t3")
    await asyncio.sleep(1)
    print("finish t3")

async def t4():
    print("start t4")
    await asyncio.sleep(1)
    print("finish t4")

if __name__ == '__main__':
    main()
========================================================================


Marko

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

how to get the list form dictionary's values davidbenny2000@gmail.com - 2016-02-21 05:34 -0800
  Re: how to get the list form dictionary's values Chris Angelico <rosuav@gmail.com> - 2016-02-22 01:04 +1100
    Re: how to get the list form dictionary's values Ho Yeung Lee <davidbenny2000@gmail.com> - 2016-02-21 06:15 -0800
      Re: how to get the list form dictionary's values Chris Angelico <rosuav@gmail.com> - 2016-02-22 02:01 +1100
        Re: how to get the list form dictionary's values Marko Rauhamaa <marko@pacujo.net> - 2016-02-21 17:16 +0200
  Re: how to get the list form dictionary's values Peter Otten <__peter__@web.de> - 2016-02-21 15:06 +0100
    Re: how to get the list form dictionary's values Ho Yeung Lee <davidbenny2000@gmail.com> - 2016-02-21 06:21 -0800

csiph-web