Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103294
| Path | csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsfeed.freenet.ag!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: how to get the list form dictionary's values |
| Date | Mon, 22 Feb 2016 02:01:37 +1100 |
| Lines | 65 |
| Message-ID | <mailman.8.1456066906.20994.python-list@python.org> (permalink) |
| References | <554c09f2-6a6b-4576-9c9d-50d6234c8583@googlegroups.com> <mailman.6.1456063449.20994.python-list@python.org> <813ad31d-b208-496f-91b0-5f39aef28965@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de oOpcS+heKYAOcY6QNgJnWQM/gV2l+rC7+OXS7khoujuw== |
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'dependency': 0.07; 'subject:form': 0.07; 'cc:addr:python-list': 0.09; '22,': 0.09; 'starter': 0.09; 'threads,': 0.09; 'thread': 0.10; 'def': 0.13; '2")': 0.16; '2016': 0.16; 'async': 0.16; 'chris,': 0.16; 'expert,': 0.16; 'finishes': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'run.")': 0.16; 'semaphores': 0.16; 'subject:values': 0.16; 'threads': 0.16; 'wrote:': 0.16; 'skip:l 30': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'fairly': 0.22; 'please?': 0.22; 'am,': 0.23; 'feb': 0.23; 'dependent': 0.23; 'finished': 0.23; 'this:': 0.23; 'import': 0.24; 'header:In- Reply-To:1': 0.24; 'mon,': 0.24; "doesn't": 0.26; 'subject:list': 0.26; 'equivalent': 0.27; 'separate': 0.27; 'message- id:@mail.gmail.com': 0.27; 'correct': 0.28; 'raise': 0.29; "i'm": 0.30; 'code': 0.30; 'task': 0.30; 'implement': 0.32; 'run': 0.33; 'requirement.': 0.33; 'running': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'done': 0.35; 'tasks': 0.35; 'something': 0.35; "isn't": 0.35; 'but': 0.36; 'there': 0.36; 'lines': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'flow': 0.36; 'subject:: ': 0.37; 'done.': 0.37; 'received:209.85.213': 0.37; "won't": 0.38; 'received:209': 0.38; 'wrong': 0.38; 'sure': 0.39; 'subject:the': 0.39; 'your': 0.60; 'matter': 0.63; 'situation': 0.67; 'expert': 0.70; 'await': 0.76; 'sounds': 0.76; 'subject:get': 0.81; '--->': 0.84; 'chrisa': 0.84; 'practicing': 0.84; "there'll": 0.84; 'to:none': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=fcUkYOFovVTsIPy6npvH4pb31JVKu1jo/NAVUDjwGrE=; b=viXnk96M4Jie+G+HnhC2o8ekSGzYQEKRORHSNeIE26b/YPlGz9uAgP7P8aj8Fkaz9r sT1Ce6jRdM/1j45s5fZGyqtPaV7uW+1rJlBnXhIcB/vNZAg1pEZx3QzJyQ0a8gcCJJAO VHxU8Fn71iaLZMUybB1ZA3DazYNb466Oadv5ov9kXCn4OZMnI8ZhPaSU0xx31FlQDHBi OWI5t+2ksp2NcJIRPx5yCRZgXgqASiiUeGYB/OialZuWF5FBwKY6kIfhjCtduiYfX912 4n5GBBAsmTnUaABir1o4H4IBVUIDzeMywf0UMHfUPSx4/jGQVWWWzz2IP2WfvWBxRPL2 hJSA== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-type; bh=fcUkYOFovVTsIPy6npvH4pb31JVKu1jo/NAVUDjwGrE=; b=WpmLWXg130U/On5hxhTUh65G+PsWVRid96W2JpKXnBr8puesG28mATUGCfkkfG2BQA 8ADqxqW6qYsuoCqCbp+pWem3SsFNHP6Lte2qF9u711E00Muak/s7zauqqIEn8RgN/1dH ha+liKVCK3865VXdQDUjVpOD1MIO03463PR8tx6J54cwJ/vvep0A88aTWRcDZOPyVG8e u4UW28zPJEV9TksRLgeQ1rIr6ABrwfHXu4wcouErYXK80R+q/Z1llRgvSRjFwmzQUITA HfHie+76dlNFWV4+3JrxBlP0yF1XQT085h8MOgepSS61JQ+3PI3jBu/76eafdHLg1MAC M7fw== |
| X-Gm-Message-State | AG10YOQJS9Qd0U3MbB3ZdbKpmRZ5fvGSDvEDlYzwMRqfRPGOzbRB+UKWPBc/m/oTzc/UuBeulAJdMle7b0fNkQ== |
| X-Received | by 10.50.43.228 with SMTP id z4mr5313498igl.94.1456066897137; Sun, 21 Feb 2016 07:01:37 -0800 (PST) |
| In-Reply-To | <813ad31d-b208-496f-91b0-5f39aef28965@googlegroups.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21rc2 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:103294 |
Show key headers only | View raw
On Mon, Feb 22, 2016 at 1:15 AM, Ho Yeung Lee <davidbenny2000@gmail.com> wrote:
> Hi Chris,
>
> 0 ---> 2 --> 3--> 1 ---> 0
> ---> 4 /
>
> i am practicing task flow in this graph situation
>
> when current state is 2 , there are 3 and 4 to run parallel and wait list of tasks finish before running to 1 ,
>
> however i feel that my code has been wrong because 3 and 4 can not combine to run task 1 ,
>
> which is the correct way to combine to run task 1?
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.
One way to do it would be something like this:
import asyncio
loop = asyncio.get_event_loop()
async def starter():
print("Starting!")
global t1, t2
t1 = asyncio.Task(dep1())
t2 = asyncio.Task(dep2())
print("Done!")
await dependent()
async def dep1():
print("Dependency 1")
await asyncio.sleep(3)
print("Dep 1 done.")
async def dep2():
print("Dependency 2")
await asyncio.sleep(2)
print("Dep 2 done.")
async def dependent():
await t1
await t2
print("Deps are done - dependent can run.")
loop.run_until_complete(starter())
The dependent task simply waits for each of its dependencies in turn.
It doesn't matter which one finishes first; it won't do its main work
until both are done. And if your starter and dependent are logically
connected, you can just have the 'await' lines in the middle of one
tidy function.
(Can an asyncio expert tell me how to tidy this code up, please? I'm
sure this isn't the cleanest.)
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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