Watch out for this (async) generator cleanup pitfall in Python

  Рет қаралды 7,701

mCoding

mCoding

Күн бұрын

Пікірлер: 33
@amarasa2567
@amarasa2567 9 сағат бұрын
So basically, don't open and close resources in your generators, but give handles to those resources to your generators? Like don't let your generator open a file itself, but give it a file handle?
@PeterZaitcev
@PeterZaitcev 7 сағат бұрын
Exactly.
@PeterZaitcev
@PeterZaitcev 6 сағат бұрын
what *could* be done with the mentioned PEP is to add support for these iterclose methods but do not activate them in any default classes, instead provide a wrapper in contextlib that you can attach to your generator functions (or classes) which adds the missing method.
@marckiezeender
@marckiezeender 16 сағат бұрын
How about instead of "don't use generators" we just say "don't use 'with' or 'finally' blocks inside of generators" Edit: what I mean is that you should avoid context and resource management inside of generators. If the generator needs access to, say, a file, then the generator's caller should be responsible for opening and closing that file.
@AbstractObserver
@AbstractObserver 14 сағат бұрын
He explain why thats not a good way to do that wat in the video
@themartdog
@themartdog 12 сағат бұрын
​@@AbstractObserver at what timestamp? I didn't hear him say that. Even if he did, I think it's a good idea to avoid with/finally in these contexts
@aaliboyev
@aaliboyev 13 сағат бұрын
As a conclusion "Wrap all your with statements with for loop inside into separate function which only holds this statement to guarantee gc"
@xelaxander
@xelaxander 16 сағат бұрын
The resource existing at least as long as the generator seems like to me like the natural solution. Imagine if it wasn’t, I.e. the generator was accessible while the resource isn’t. Suddenly it’s even more cursed, since you can try to get an element from the generator, but it indeterminately fails to yield.
@xelaxander
@xelaxander 15 сағат бұрын
Resources not being garbage-collected immediately is a choice necessary to allow alternative Python implementations with different optimization goals. That said, at least in CPython, you can also just del g, and then gc.collect(). Although I’m not sure if the Python spec guarantees cleanup.
@DrGreenGiant
@DrGreenGiant 10 сағат бұрын
​@@xelaxanderyeah my question would be, is it in the spec or is it an implementation detail. If the latter then I'd happily be that guy who says it sounds like a non cpython implementers problem.
@aaronm6675
@aaronm6675 16 сағат бұрын
Wild
@endoflevelboss
@endoflevelboss 16 сағат бұрын
James Murphy for new BDFL 🎉
@RogerValor
@RogerValor 9 минут бұрын
Not sure how often I care about when resources are cleaned up, as long as they are cleaned up. But I guess this is good to know for the case when I encounter it. So thx!
@motbus3
@motbus3 6 сағат бұрын
You can also use a context manager which encloses both generator and resources
@motbus3
@motbus3 6 сағат бұрын
But that's annoying
@janhwillems10000
@janhwillems10000 6 сағат бұрын
Thanks a lot James!
@idoben-yair429
@idoben-yair429 6 сағат бұрын
The ACTUAL solution is to write your code such that it doesn't rely on exactly when resources get cleaned up, and if the exact time of the cleanup is important, then express it EXPLICITLY in the code by calling a cleanup method. RAII-style context managers are not the right tool when the order and timing of cleanup is important. Generators are not the issue here. The engineering approach is the issue. To elaborate: RAII means Resource Acquisition Is Initialization. The converse, i.e., Reference Loss Is Resource Disposal, does NOT hold. This is an important implication of the reference counting style of resource management, unlike C++-style RAII where you can engineer your classes to enforce both RAII and RLIRD.
@ilmuoui
@ilmuoui 4 сағат бұрын
Sounds like Rust with extra added steps
@idoben-yair429
@idoben-yair429 4 сағат бұрын
@@ilmuouievery language has different tools, stop worshipping languages, they’re just that - tools
@themartdog
@themartdog 2 сағат бұрын
​@@ilmuoui rust itself is programming with extra stepa
@danwellington3571
@danwellington3571 14 сағат бұрын
Obligatory Rust comment that completely ignores the 30-odd years of design choices Python has to do its best to not break
@DrGreenGiant
@DrGreenGiant 10 сағат бұрын
Works fine in assembly too
@tc14hd23
@tc14hd23 16 сағат бұрын
No problem was ever solved by adding 'async' in front of it
@fyaa23
@fyaa23 6 сағат бұрын
Why do you expect garbage collection to behave like RAII in other languages? The point is that you don't have to take care to clean it up, but you can hardly control when it happens.
@tophat593
@tophat593 16 сағат бұрын
Sorry, beyond tired and ever so slightly drunk. Want to listen but need to sleep and tomorrow I won't remember this video exists.
@exploited410
@exploited410 15 сағат бұрын
Answering this so you have a notification tomorrow and will see this video on a clear mind
@MarianoBustos-i1f
@MarianoBustos-i1f Сағат бұрын
wake up and watch the video.
@MrAlanCristhian
@MrAlanCristhian 15 сағат бұрын
That is the type of behavior that is detected with unit testing.
@austinnar4494
@austinnar4494 12 сағат бұрын
That's not true, the async case is literally non-deterministic. So you could have application code that expects the resource to be cleaned up, the unit tests pass, but then fails in production.
@MrAlanCristhian
@MrAlanCristhian 12 сағат бұрын
@@austinnar4494 No, in that case the test will randomly fail. Because the test suit it's supose to run multiple times before deployment. The test suit runs every time a change is made.
@themartdog
@themartdog 12 сағат бұрын
​@@MrAlanCristhian but it could randomly fail only .01% of the time...
@SpeedingFlare
@SpeedingFlare 16 сағат бұрын
Just use C and do if(error) goto cleanup; where cleanup cleans up anything that's non-null in whatever order you want. For async, I don't know
Avoid These BAD Practices in Python OOP
24:42
ArjanCodes
Рет қаралды 50 М.
AI can't cross this line and we don't know why.
24:07
Welch Labs
Рет қаралды 704 М.
отомстил?
00:56
История одного вокалиста
Рет қаралды 6 МЛН
Running With Bigger And Bigger Lunchlys
00:18
MrBeast
Рет қаралды 15 МЛН
Men Vs Women Survive The Wilderness For $500,000
31:48
MrBeast
Рет қаралды 101 МЛН
小丑妹妹插队被妈妈教训!#小丑#路飞#家庭#搞笑
00:12
家庭搞笑日记
Рет қаралды 36 МЛН
Async for loops in Python
16:36
mCoding
Рет қаралды 63 М.
The unexpected probability result confusing everyone
17:24
Stand-up Maths
Рет қаралды 577 М.
Vim Tips I Wish I Knew Earlier
23:00
Sebastian Daschner
Рет қаралды 65 М.
Modern Python logging
21:32
mCoding
Рет қаралды 186 М.
Why Everyone Loves Zustand
29:27
Theo - t3․gg
Рет қаралды 76 М.
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 226 М.
Generics: The most intimidating TypeScript feature
18:19
Matt Pocock
Рет қаралды 175 М.
BlackRock: The Conspiracies You Don’t Know
15:13
More Perfect Union
Рет қаралды 398 М.
отомстил?
00:56
История одного вокалиста
Рет қаралды 6 МЛН