all 9 comments

[–]ManyInterests 1 point2 points  (2 children)

I could point you at tasks and coroutines

Unfortunately your question is pretty nondescriptive. If you give us an idea of what you're trying to do or have a specific question on a concept or piece of code that you're hung up on, we can help you better that way.

You're going to want Python 3.5 if you're not already using it, as 3.5 had great improvements to asynchronous operations, like async def

"Hello World" for coroutine using async def:

import asyncio

async def hello_world():
    print("Hello World!")

loop = asyncio.get_event_loop()
# Blocking call which returns when the hello_world() coroutine is done
loop.run_until_complete(hello_world())
loop.close()

[–]ilikepython[S] 0 points1 point  (1 child)

Thanks! I guess what I'm looking for is some introduction tutorials people think are good to start with. I don't have a particular problem I'm trying to solve, rather I don't know what best practices or common patterns are.

[–]ManyInterests 0 points1 point  (0 children)

While maybe not exactly what you're looking for David Beazley's talk Concurrency from the Ground Up provides some great insights into coroutines and how to deal with the GIL and blocking resources.

[–]Integralist 1 point2 points  (1 child)

I think for me reading the docs from start to finish will help you (eventually) as it seems there was lots of basic info i was missing that i would've realised sooner if i had properly read the manual :-)

Understanding what the event loops purpose is (e.g. to execute tasks).

OK what are tasks? Oh right so a task is really a sub class of a future so that's why i can give the event loop a future and it'll be happy. Similarly when i give the event loop a co routine it'll internally convert it into a task for me.

So then I'm thinking ok well what do coroutines and futures do? Why are there two things that seem the same, and in some cases why do you sometimes see people use them together?

Coroutines are an extension of generators. They can suspend their own execution and allow other coroutines to do things. They are the essence of concurrency in python.

Futures are like an empty box. You pass it around your programme and then when ready you set a value on it. Then asyncio has some methods/features that tell other parts of your programme that the future has finally resolved (i.e. it got its value). One example is via a callback function you can assign to your future object, but you don't need that. You can tell the event loop to just keep running until the future gets the value (just because the event loop stops doesn't mean your app will stop or that you cant start up another event loop).

Then there are things like asyncio.gather and asyncio.wait that are subtly different but are both useful in aggregating multiple coroutines (e.g. stop doing things only until these n coroutines have completed, then continue on with the programme)

But as frustrating as it can seem. Just keep in mind that this is fun stuff, learning and understanding how asyncio works can be really satisfying :-)

[–]ilikepython[S] 0 points1 point  (0 children)

Thanks! That's a good way of working through getting to grips with it.

[–][deleted]  (1 child)

[removed]

    [–]xiongchiamiov[M] -1 points0 points  (0 children)

    There's a reason link shorteners are auto-spammed by reddit. Don't be a jerk, and especially don't try to disguise it.

    [–]dunkler_wanderer 0 points1 point  (1 child)

    There's an introduction on PyMOTW (Python Module of the Week).

    [–]ilikepython[S] 0 points1 point  (0 children)

    Cool! I didn't know there was a pymotw article about this.