The idea of explaining async by printing out real world tasks is genius
@unperrier Жыл бұрын
you must've already known async concepts and/or specifically asyncio because if I were a noob and didn't know much about async, let alone asyncio in particular, I'd be lost. Especially if I wasn't familiar with python language in the first place. At the very least I'd have to go through the video multiple times and read more wherever mCoding is glossing over central concepts like event loop, scheduling and a coroutines.
@eeriemyxi Жыл бұрын
It should not be considered a genius thing to do; every video that is explaining an abstract notion should try its best to show its uses in the real world. I think the content creators on KZbin who are just here for the fat wallets have caused this unfortunate lower expectation from the average educational video.
@eeriemyxi Жыл бұрын
@@unperrier I don't agree that it is always necessary to have a textual definition known understandably to learn a concept clearly. Even if they didn't explain what _coroutines_ are, they did show what it is; and I think that is still easier to understand than all those fancy words used by authors of many articles on any topic. Although my argument is not that this is _always_ the case; in a fair share of concepts, it is better to read some article than to just see how it all works in an intended comprehensive manner.
@unperrier Жыл бұрын
@@eeriemyxi James is too fast, he speaks with a high pace, may be okay for native English speakers but we're not all lucky. He also doesn't explain very well, unfortunately. It's missing visual cues as to how the tasks are scheduled and a graph of messages going through the queues would help. Personally I don't need it because I've been doint that for years, but I had difficulties following the pace of the video. So I imagine noobs will struggle.
@minernooberz68 Жыл бұрын
@@unperrier I agree I do not think a beginner would understand this video, but I think JavaScript would teach you async await better
@packetpunter Жыл бұрын
having additional things to try out at the end of the video is awesome. your content is always so great! a sincere thank you :)
@mCoding Жыл бұрын
It's a great complement as an instructor that my students want to see the homework!
@Aceptron Жыл бұрын
@@mCoding Yes, it is absolutely helpful!!!
@Phaust942 жыл бұрын
This channel is like top 3 channels for Python on the web. Thanks for this and keep it up!
@abdelghafourfid8216 Жыл бұрын
what are other two ?
@mjaysmileofficial Жыл бұрын
@Abdelghafour Fid I'm not this guy, but Arjan Codes is pretty good. He explains things using more close to real-world examples
@notead Жыл бұрын
@@mjaysmileofficial I feel like the quality of his videos has declined. Giving heavy content crater vibes now
@mjaysmileofficial Жыл бұрын
@Note Maybe it is. I haven't watched his latest content, but the videos about async python are pretty good
@Just2Dimes Жыл бұрын
@@abdelghafourfid8216 besides Arjan Codes, which has been mentioned above, Corey Schafer and Anthonywritescode are worth looking into - if you didn't know them already.
@WebMafia Жыл бұрын
wow, this is by far the best asyncio tutorial I have encountered. thank you!
@Cucuska22 жыл бұрын
Great video, for a while I have been hoping that I would just stumble upon a Python-specific async tutorial.
@quitethecontrary1846 Жыл бұрын
Thank you...the idea of waiting on multiple things at once is what made it "click" for me
@lawrencedoliveiro91042 жыл бұрын
4:46 You can have a nonempty pending list even in the absence of a timeout. There is another optional argument to asyncio.wait() which specifies whether to wait for all tasks to complete (the default), or just the first one.
@lawrencedoliveiro91042 жыл бұрын
2:53 time.sleep() blocks the current thread, asyncio.sleep() only blocks the current task. Remember that a “task” is not a concept of the Python language itself: it is purely a figment of the asyncio library -- it is a wrapper around a coroutine object, which _is_ a concept of the Python language. Tasks are schedulable entities managed by asyncio.
@Graham_Wideman Жыл бұрын
Am I right to infer that your point is that sleep() blocks the OS thread in which Python is running, hence Python has no way to do anything else while the OS has Python suspended?
@ed_iz_ed Жыл бұрын
sleep blocks execution at the python level, while being non blocking at the os level, this means the actual thread can be used for something else, just not by your python program
@hriscuvalerica48142 жыл бұрын
Everything i needed and more . Async content is lacking on KZbin.
@CritiKaster Жыл бұрын
Dude your videos are so great - making hard stuff easy - thanks!
@mCoding Жыл бұрын
I appreciate the praise!
Жыл бұрын
Wow, this is crazy similar to Rust! The difference is pretty much only function names. And such a clear explanation with a good analogy, I can share this with Rust newbies.
@probaddie456 Жыл бұрын
10:54 You can't parse [X]HTML with regex. Because HTML can't be parsed by regex. Regex is not a tool that can be used to correctly parse HTML. As I have answered in HTML-and-regex questions here so many times before, the use of regex will not allow you to consume HTML. Regular expressions are a tool that is insufficiently sophisticated to understand the constructs employed by HTML. HTML is not a regular language and hence cannot be parsed by regular expressions. Regex queries are not equipped to break down HTML into its meaningful parts. so many times but it is not getting to me. Even enhanced irregular regular expressions as used by Perl are not up to the task of parsing HTML. You will never make me crack. HTML is a language of sufficient complexity that it cannot be parsed by regular expressions. Even Jon Skeet cannot parse HTML using regular expressions. Every time you attempt to parse HTML with regular expressions, the unholy child weeps the blood of virgins, and Russian hackers pwn your webapp. Parsing HTML with regex summons tainted souls into the realm of the living. HTML and regex go together like love, marriage, and ritual infanticide. The cannot hold it is too late. The force of regex and HTML together in the same conceptual space will destroy your mind like so much watery putty. If you parse HTML with regex you are giving in to Them and their blasphemous ways which doom us all to inhuman toil for the One whose Name cannot be expressed in the Basic Multilingual Plane, he comes. HTML-plus-regexp will liquify the nerves of the sentient whilst you observe, your psyche withering in the onslaught of horror. Rege̿̔̉x-based HTML parsers are the cancer that is killing StackOverflow it is too late it is too late we cannot be saved the transgression of a chi͡ld ensures regex will consume all living tissue (except for HTML which it cannot, as previously prophesied) dear lord help us how can anyone survive this scourge using regex to parse HTML has doomed humanity to an eternity of dread torture and security holes using regex as a tool to process HTML establishes a breach between this world and the dread realm of c͒ͪo͛ͫrrupt entities (like SGML entities, but more corrupt) a mere glimpse of the world of regex parsers for HTML will instantly transport a programmer's consciousness into a world of ceaseless screaming, he comes, the pestilent slithy regex-infection will devour your HTML parser, application and existence for all time like Visual Basic only worse he comes he comes do not fight he com̡e̶s, ̕h̵is un̨ho͞ly radiańcé destro҉ying all enli̍̈́̂̈́ghtenment, HTML tags lea͠ki̧n͘g fr̶ǫm ̡yo͟ur eye͢s̸ ̛l̕ik͏e liquid pain, the song of re̸gular expression parsing will extinguish the voices of mortal man from the sphere I can see it can you see ̲͚̖͔̙î̩́t̲͎̩̱͔́̋̀ it is beautiful the final snuffing of the lies of Man ALL IS LOŚ͖̩͇̗̪̏̈́T ALL IS LOST the pon̷y he comes he c̶̮omes he comes the ichor permeates all MY FACE MY FACE ᵒh god no NO NOO̼OO NΘ stop the an*̶͑̾̾̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s ͎a̧͈͖r̽̾̈́͒͑e not rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ
@mCoding Жыл бұрын
You got the reference :)
@prringa6099 Жыл бұрын
Nicely copied from StackOverflow. ;)
@charlesclampitt7865 Жыл бұрын
This post looks exactly as it is supposed to look - there are no problems with its content. Please do not flag it for our attention.
@brycedevoe98142 жыл бұрын
The best advice I can give to anyone regarding python's async/asyncio is to read the documentation, as there are a lot of edge cases and it's well documented. For example you need to save objects from create_task with a class variable or they get garbage collected
@unperrier Жыл бұрын
yeah the weak reference can be a problem. Overall I find the asyncio API too low-level to be really useful. I've written applications and once you mix multiprocessing (external processes having their own ioloop) it becomes too complicated, you have to manage everything yourself. to give an example it's like if python only exposed an API for threading where you have to create the thread yourself, create local storage and manage a queue for each thread yourself. That'd be terrible to use... well asyncio is at that level imo, not high enough (yet) to be awesome.
@unusedTV2 жыл бұрын
I did not know it had a built in HTML parser! Then why do so many people reach for beautifulsoup? Can you do a comparison, and explain if they are or aren't used for the same goals?
@eeriemyxi Жыл бұрын
I personally use Selectolax due to its speed and CSS selector. I don't know much about the built-in HTML Parser, but I don't think it has CSS selectors.
@Graham_Wideman Жыл бұрын
BeautifulSoup is not a parser, it relies on your choice of html parsers (possibly Python's built-in one) to do its work. What BeautifulSoup adds is a suite of methods to navigate and search the resulting document tree to extract the information you want.
@maxthomarino6 ай бұрын
incredible educational content, you are a great teacher because you have so much experience
@Al-.-ex2 жыл бұрын
Great intro to async, though definitely feels like something I need to do myself a couple times before I understand it/remember it fully. Really appreciate the suggestions at 13:18 for that!!
@ziggyspaz Жыл бұрын
Hands down the best async python tutorial
@mCoding Жыл бұрын
Wow, thanks!
@Varuns36 Жыл бұрын
i hit subscribe within first 40 secs, i knew finally i am going to understand this anync maddness
@efaile34312 жыл бұрын
Yes, HTML is not a regular language, however your problem might be. For example, if you are searching for the only id="..." on the page, you can totally do that with a regex.
@DrewTNaylor2 жыл бұрын
Oh no, parsing HTML with regex...
@ConstantlyDamaged Жыл бұрын
I feel that it's at this point you make soup. Delicious, beautiful soup.
No, still not worth it. You'll find plenty of pages that will parse in a browser but fail horribly if you attempt to apply a regex to link extraction. You'll find unexpected attributes, malformed syntax, and so many other problems. You can't even imagine how gross vaguely parsable HTML can get. You need a parser, and regexes are not parsers.
@clouck59 Жыл бұрын
I thought I was experienced in python. I discovered "await" and "async" with this video.
@ali-om4uv Жыл бұрын
Wow that is some great content. But for me some of the webcrawling concepts are a little distracting. It would be so great if you could add a video for async work on Datapipelines ( get some csv files from an fftp, transfrom them and bulk load them to an relational db for example including logging , taks handling and retry logic). I think this would be a great lesson for many data scientists who are not really educated in data eneneering topics. Anyhow thanks for the homework, i hope i will not fail it :-)
@mCoding Жыл бұрын
Great suggestion! Since data pipelines are often compute-bound rather than io-bound, multiprocessing is also a common option to use instead of asyncio. Check out my video on unlocking all your cores where I show a basic ETL workflow using multiprocessing.
@darske1 Жыл бұрын
I remember one day I saw an example of this with a script that downloads videos from internet, instead of waiting to download each one sequentially, they used asyncio, but I was wondering, if the downloads happen simultaneously, won't that give less bandwidth to each video and then making each one download slower? How is it still giving benefits with this approach? (Or maybe my understanding of how things are downloaded is not right hehe)
@mdemdemde Жыл бұрын
I assume it depends on the bottleneck in download speed. If the speed is uncapped, I think what you say is true, but if the server limits the download speed you should improve using asyncio. Does this make sense?
@darske1 Жыл бұрын
@@mdemdemde Completely, thanks for your answer! :D
@omgwtfafterparty Жыл бұрын
I am looking for tips on how to properly use logging module with asyncio. Supposing all the coroutine functions emit some log messages, it will quickly become very difficult to find out which log message comes from which task as they are all executed simultaneously. Or am i missing something? :)
@mCoding Жыл бұрын
If you want to know what task each message is associated with, include an identifier of the task (like its name) in the log message.
@nikolausluhrs Жыл бұрын
I have been looking forward to this
@aprilli5315 ай бұрын
is it safe to access the same integer and set across multiple coroutines though?
@lethalavidity Жыл бұрын
- Am I awaited? - You are awaited, shiny and chrome!
@Khushpich Жыл бұрын
Great video as always
@royz_1 Жыл бұрын
The syntax seems to have improved a bit since I last tried async programming in python 3.6 Still javascript has the best syntax for async programming in my opinion.
@SirDonald Жыл бұрын
Wow that's crazy I was just searching for this yesterday
@HerChip Жыл бұрын
this async does not directly imply multithreading/core, right?
@mCoding Жыл бұрын
Correct. We are only using 1 thread on 1 core and only 1 process.
@b33thr33kay9 ай бұрын
Frankly, if you've never done asynchronous programming, this video not very introductory. It's taking me several days to understand how it all works. However, if you have the time to understand the fundamentals, then come back to this example, this is a good video. If you're confused by the Queue thing: put() and get() are coroutines because they might yield back control to the event loop if the queue is full or empty, respectively. Internally, they create and store a future then await on it: awaiting on a future yields control to the event loop (it's literally a "yield" statement). The opposite method then resolves the future by calling the future's set_result() method, which unblocks the first method.
@lawrencedoliveiro91042 жыл бұрын
Another interesting topic would be using asyncio and coroutines in GUI programming. Because every GUI framework implements its own event loop. The key thing about asyncio is it provides a standard event-loop API for Python. You can replace its default event loop with wrappers around alternative event loops, and thereby use coroutine tasks in GUI apps.
@mattlau04 Жыл бұрын
Very interesting video, really helps understand how async works
@abdelghafourfid8216 Жыл бұрын
if someone has done the improvements suggested at the end I would love to check out the final version
@ConstantlyDamaged Жыл бұрын
An excursion into my usual playground! Very nice and succinctly explained. Perhaps another related one would be the concept of async wrappers?
@unperrier Жыл бұрын
Nice, but I find your example too complicated for a video. And I'm an experienced async programmer. I can follow, but at least a diagram with the queues would be helpful for global comprehension. And a diagram of how the ioloop schedules coroutines, because there are a lot of assumptions and I believe it's hard for beginners to follow without visual cues.
@Graham_Wideman Жыл бұрын
While async is not threading, it would be great if James could clarify how different tasks may communicate properly, for example what happens with variables shared between tasks.
@talideon Жыл бұрын
It's in-process cooperative multitasking, so assuming you don't hand off to another task in the middle of mutating something, it's fine. Not good, but fine.
@joshinils Жыл бұрын
The biggest problem I had with trying to implement concurrency is working on the same data from different threads. i.e. multiple threads insert into the same dict/list when they are done with their task.
@Musava Жыл бұрын
You shouldn't mix threading and async, as explained asyncio should be used when you're slowed down by IO (waiting for things over the internet or hardware jobs), and threading when you actually need concurrency. Asyncio is not concurrent, it is single threaded, it's just more efficient time management (most ideally said, there's always something running)
@joshinils Жыл бұрын
@@Musava right, but the same issue applies to both, doesn't it? And i was waiting on requests from the internet, and parsing the results.
@Musava Жыл бұрын
@@joshinils Nope, with pure async you will never have two functions running concurrently. Imagine looking at your code and you have a laser pointer, if you use only asyncio, you will always be able to track where the code currently is at. It may be a lot of jumping but it executes only one function at a time. If you'd use threading, you'll need more pointers. In the web crawler example, imagine a ideal internet where it takes exactly 5 seconds to load and the data is huge so it is 0.5 seconds to parse the data. Asyncio will send all requests and then wait, since they all will respond at the same time, you will only be able to parse one at a time, because the parsing is not bottlenecked by connection, or anything, (code never waits in the parsing for anything, it is just doing heavy work). Edit: thats why you use threading or sometimes even multiprocessing instead of async in heavy computation. Async is to save time from waiting (use the time at which code would wait for something), threading is to use more of your pc power
@volbla Жыл бұрын
The task/worker doesn't need to update the collection itself. You can put the parsed result in a queue and update the list synchronously. I think.
@macampo Жыл бұрын
great video, thank you very much
@trag1czny2 жыл бұрын
finally an async video 🤩 discord gang 🤙
@MocSomething Жыл бұрын
Does the HTML parser also support XML? Is there also one for JSON??
@eeriemyxi Жыл бұрын
You can use the `xml` and `json` built-in libraries for such use cases.
@volbla Жыл бұрын
Have you tried to parse json without importing json? Oh no... you poor soul.
@MrYyope15555Ай бұрын
Amazing Sir
@VirtuousSaint Жыл бұрын
any particular reason you didn't use aiohttp?
@akhileshchander530710 ай бұрын
very common error: 32 if events._get_running_loop() is not None: ---> 33 raise RuntimeError( 34 "asyncio.run() cannot be called from a running event loop") then use: await main()
@unperrier Жыл бұрын
I've written asyncio applications and it quickly becomes complicated once you mix multiprocessing (external processes having their own ioloop), listening on sockets and pipes (each having slightly different implementation that requires you to write different code for each), your code becomes too complicated, you have to manage everything yourself and navigate abstractions that do the same things but are different (reading lines coming from sockets vs pipes). To give an comparison, it's like if python only exposed an API for threading where you have to create the thread yourself, create local storage yourself and schedule work to run in the thread via a queue, for every thread, all by yourself. That'd be terrible API to use... well asyncio is at that level imo, not high enough (yet) to be awesome.
@talideon Жыл бұрын
It's in-process cooperative multitasking. None of this is surprising when you're familiar with the constraints around cooperative multitasking.
@caongocdavidlong2 жыл бұрын
Please do a video on the aiomultiprocess library.
@arjix87386 ай бұрын
For async crawling I prefer rust with tokio, really nice stuff.
@PeterZaitcev Жыл бұрын
Well, nothing is wrong in the original problem. Your only cook's hat got dirty and thus you bring it to the laundry, but without it you can't bake. However, today somewhere in between 10:00 and 15:00 you've got a courier delivery and thus must be at home. That leads to the situation when you first wait until the Amazon delivery (task with 3h timeout), then you go to the laundry and wait until your clothes wash and dry, and only then you can start baking a cake.
@jgtb0pl2 жыл бұрын
I'm hyped up
@yomajo Жыл бұрын
5yrs in python, never did threading, multiprocessing or async. Am I alone? Keep postponing learning it, seems too difficult.
@chndrl5649 Жыл бұрын
How much does it cost for cpu? Im actually curious.
@sonice9020 Жыл бұрын
1 night with your mother
@lawrencedoliveiro91042 жыл бұрын
1:57 Best to call it a “coroutine object”.
@HoSza1 Жыл бұрын
Web crawlers should handle AJAX (XMLHttpRequest) generated dynamic content, shouldn't they? (I wonder how much of the total content is available these days, I guess not a trivial amount of percentage...) Nice video BTW.
@mCoding Жыл бұрын
You are certainly welcome to have your hand at implementating it :)
@HoSza1 Жыл бұрын
@@mCoding When I try to implememt about anything I just find that someone has already did it, or they tell me why am I reinventing the wheel, so frustrating. :) The existing solution probably is that a headless browser gets queried for the page source or the DOM document for the page in question.
@Graham_Wideman Жыл бұрын
@@HoSza1 That's what Selenium does for you.
@DanielMaidment Жыл бұрын
Nice intro, a bit fast into those queues.
@_ramen Жыл бұрын
great vid
@re.liable Жыл бұрын
"asyncio is very much an afterthought" so true :( I did my own delve into asyncio very recently and I didn't have a good time. Maybe it's just my poor docs reading skills tho
@mCoding Жыл бұрын
I think the docs could really use some improvement. Most Python docs are very understandable but I feel like the async docs are a bit chaotic.
@CrushedAsian2557 ай бұрын
I made my own “asyncio” using an array of generator functions and while fun(): yield nonsense
@henrybigelow3570 Жыл бұрын
why not have one task for each crawl() and have crawl call asyncio.create_task(crawl(url)) for each found url? then you don't need worker functions or a queue.
@grzegorzryznar51012 жыл бұрын
Btw this background looks very gray comparing to this one from new UI
@mikhaililin5534 Жыл бұрын
What would be the advantage of using async instead of Thread? from threading import Thread import time import random def slow_function(thread_index): time.sleep(random.randint(1, 10)) # simulates waiting time for an API call response for example print("Thread {} done!".format(thread_index)) def run_threads(): threads = [] for thread_index in range(5): individual_thread = Thread(target=slow_function, args=(thread_index,)) threads.append(individual_thread) individual_thread.start() # at this point threads are running independently from each other and the main flow of application print("Main flow of application") for individual_thread in threads: individual_thread.join() # joining threads insures that all threads are done before moving further in the flow of application print("All threads are done") run_threads() p.s. great video as always!
@mCoding Жыл бұрын
There are two main benefits (tradeoffs). Firstly, async is cooperative whereas threads are not, meaning that when using async the only time when you have to worry about someone else interrupting your execution and seeing potentially broken invariants is when you explicitly say "await" or other explit waiting mechanism. In contrast, a thread could be interrupted anywhere at any time, and you must use locks to avoid other threads seeing broken invariants or racing for data. The second tradeoff is how heavy a thread is versus a coroutine. Threads are much more heavy-duty objects, and your system may grind to a halt if you create more than a few thousand threads. You can have millions of coroutines pending without issue, though.
@mikhaililin5534 Жыл бұрын
@@mCoding Thank you for such a fast and extensive reply! From here I can dig deeper in Cooperative vs Preemptive multitasking subject. I was not aware it was a thing until now.
@dan00b8 Жыл бұрын
"it doesnt matter if you use c or python, facebook wont respond any faster" rustaceans be like: lets rewrite whole facebook in rust now
@mingqingTeng Жыл бұрын
good async
@haroombe123 Жыл бұрын
Please make one for fast api calls. And not some low latency Wikipedia api calls
@MithicSpirit2 жыл бұрын
Discord gang!
@charlesclampitt7865 Жыл бұрын
How did you fight off Tony the Pony?
@mCoding Жыл бұрын
Commented on the wrong video?
@charlesclampitt7865 Жыл бұрын
@@mCoding It's a reference to the most famous SO question of all time. I'm pretty sure you know it.
@Jeyekomon Жыл бұрын
I really try but somehow the async programming is just too much for my brain.
@aeoteng Жыл бұрын
Title: "Intro to async..." ..."I claim that you already understand asynchronous programming" Yes, but no
@JadeJuno2 жыл бұрын
0:15 ... welp... another video I just cannot watch.
@bettercalldelta2 жыл бұрын
notification gang
@SP-db6sh Жыл бұрын
Waiting for part 2: adding this scrapped data into GraphQL with strawberry orm( source can be : stackoverflow issues)...
@khuntasaurus88 Жыл бұрын
3:35 is why I think asyncio in python is pointless. You can EASILY replicate this behavious with more control by multithreading or multiprocessing. It will be more readable too
@phenanrithe Жыл бұрын
Why would it be pointless? Coroutines don't have the overhead of multithreading (let alone multiprocessing), which is actually why they came to be in the first place. Then threads are limited by the number of cores.