Python for .NET Developers Transcripts
Chapter: async and await in Python
Lecture: Unsync to the rescue
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
I told you there is a much better API that we get to use and it's incredibly simple to bolt on to Python. There's a library called Unsync
0:09
by a guy named Alex Sherman and Alex said hey I'm working with the Python async and await and it's just rubbing me the wrong way
0:17
because he has a background from C# and .NET and he expected it to be similar. So let's look here. I'm going to go on a little rant about
0:23
async and await and Python, what's wrong. And Python 3.5, we got support for async and await. Unfortunately, I've been having trouble adapting
0:32
to Python's version of async and await. Especially coming from C#'s implementation of the tasks parallel library.
0:38
There are two big friction points that I have that are it's difficult to fire and forget an asynchronous method
0:43
you can't just call it and let it fade off into the background and run, right? That doesn't work. You have to go to the event loop
0:49
call run and help complete. That's a problem, right? You have to wait for that to finish or you can start it but eventually you still have to juggle
0:55
this loop as we saw. The other one is, you can't go remember in the C# one I said, you know, result. Basically, dot result, on the object
1:05
to make it block the first synchronous version. Well in Python, if you try to do that it'll throw an exception and say hey I'm not done yet
1:12
like could you just wait for me? I want to be done, right? So this library does that and several other actually really impressive things.
1:20
So we're going to use this library to make Python's async and await much more like C#'s. Which again, I fully acknowledged is better
1:29
for, basically, the internal run time and the tasks parallel library and the scheduling and all that and also unify some of the threading APIs
1:37
like asyncio threading and multiprocessing to achieve some really interesting things as well. So if you're coming from C#
1:45
you definitely want to make use of this library. You can pip install it and it's really simple.