Python for the .NET developer Transcripts
Chapter: async and await in Python
Lecture: Unsync to the rescue
0:00 I told you there is a much better
0:01 API that we get to use
0:03 and it's incredibly simple to bolt on to Python.
0:06 There's a library called Unsync
0:08 by a guy named Alex Sherman
0:10 and Alex said hey I'm working with
0:12 the Python async and await
0:14 and it's just rubbing me the wrong way
0:16 because he has a background from C# and .NET
0:18 and he expected it to be similar. So let's look here.
0:21 I'm going to go on a little rant about
0:22 async and await and Python, what's wrong.
0:25 And Python 3.5, we got support for async and await.
0:28 Unfortunately, I've been having trouble adapting
0:31 to Python's version of async and await.
0:33 Especially coming from C#'s implementation of
0:35 the tasks parallel library.
0:37 There are two big friction points that I have that are
0:39 it's difficult to fire and forget
0:41 an asynchronous method
0:42 you can't just call it and let it fade off
0:44 into the background and run, right?
0:46 That doesn't work. You have to go to the event loop
0:48 call run and help complete. That's a problem, right?
0:51 You have to wait for that to finish
0:52 or you can start it
0:53 but eventually you still have to juggle
0:54 this loop as we saw.
0:56 The other one is, you can't go
0:58 remember in the C# one I said, you know, result.
1:01 Basically, dot result, on the object
1:04 to make it block the first synchronous version.
1:06 Well in Python, if you try to do that
1:08 it'll throw an exception and say hey I'm not done yet
1:11 like could you just wait for me?
1:12 I want to be done, right?
1:14 So this library does that and several other
1:17 actually really impressive things.
1:19 So we're going to use this library
1:21 to make Python's async and await
1:24 much more like C#'s.
1:25 Which again, I fully acknowledged is better
1:28 for, basically, the internal run time
1:30 and the tasks parallel library
1:31 and the scheduling and all that
1:33 and also unify some of the threading APIs
1:36 like async IO threading and multiprocessing
1:40 to achieve some really interesting things as well.
1:42 So if you're coming from C#
1:44 you definitely want to make use of this library.
1:46 You can PIP install it and it's really simple.