I forgot where exactly but I did use ExitStack when developing aiosmtpd
@mrmultitask59213 күн бұрын
one of the points that sticks out to me is that sometimes it reports a time, sometimes its 0, where the actual time is actually a fraction of a second when it is measured. This smells of SMT/Hyperthreading taking away execution time and then suddenly popping everything into the foreground for your particular thread. Maybe try disabling SMT and see if things change?
@b_0rk3 күн бұрын
"it's still in production" industry standards bite again
@spencer37523 күн бұрын
Oh man. For internal calls to user-facing APIs that may invoke a warning, I've been doing things like `warnings.catch_warnings` and re-emitting warnings to correct the stack level. This seems a lot more convenient.
@yorailevi67473 күн бұрын
I don’t think a lot of people would understand why the new code is written that specific way, I rather do the if outside and conditionally enter the context with a plainer looking code and 2 duplicate returns
@PanduPoluanКүн бұрын
That's why code comments is important. Just describe the "why" to prevent others from doing the same one-liner mistake.
@MakeDataUseful4 күн бұрын
Very clever, thanks for sharing
@iliya-malecki4 күн бұрын
Define a function that works on pre-opened file descriptors and manage opening outside? Why switch between very different and fragilely stateful types - I mean it's simple for me to say that once I see the important difference between the two :)
@kamurashev4 күн бұрын
One of the only in python videos 😂
@preocts40864 күн бұрын
The number of times I've seen or left PR comments to the tune of: "This is some clever code. Can't find the bug, but I know it's there somewhere" is more than one. That's too many. xD
@prosodyspeaks40364 күн бұрын
surprised to hear about perpetual issue of conditional contexts - isn't this what nullcontext is for? i've used it successfully to conditionally manage seek-offsets and temporary-filters for db cursor management. smth like this? i dont like doing two checks for file=None, but maybe there's another implementation and in any case definitely think nullcontext should be mentioned! import sys from contextlib import nullcontext def read_first(file: str | None = None) -> str: cm = open if file is not None else nullcontext with cm(file) as stream: stream = stream or sys.stdin return next(iter(stream)) edit: def read_first(file: str = None) -> str: file, cm = (file, open) if file else (sys.stdin, nullcontext) with cm(file) as stream: return next(iter(stream))
@PanduPoluanКүн бұрын
You lose the ability to specify different parameters if you simply use nullcontext() What Anthony showed is a generalized solution. Sure you can use functools.partial() but that adds too much "cleverness" to the code.
@qexat4 күн бұрын
ohh neat trick
@ironmonkey19904 күн бұрын
thank you!
@BabakFiFoo4 күн бұрын
Anthony, you are the best. Please keep up with such content!
@mCoding4 күн бұрын
The performance counters in most VM software are simulated. At the moment the VM should simulate a virtual timer interrupt, it may not be running because your OS is running another thread. This can cause a backlog of timer interrupts, causing the guest OS to perceive substantially less time passage than the host, which can cause many weird behaviors, including duplicate values returned from monotonic clocks on operations you'd expect to take longer (e.g. Python code). Some VM software allows to pass through the host performance counters, look for the option "virtualize performance counters" or similar, although it requires hardware support. However, when using this option, you are now also perfing the VM software in addition to your Python code, so times would be artificially inflated, the best way to get accurate timing information is to use bare-metal dedicated hardware with nothing else running and a single CPU. Also, it's not much of an issue in Python, but you can observe monotonic clocks returning duplicate values when operations actually take less than the clock resolution amount of time to pass. E.g. if you try to benchmark malloc in C, which can in nice cases take ~20ns, you will find the same issue of duplicate monotonic clock values not because of some weird error but because not enough time has passed to increase the perf counter since the last call (it might take 200ns). The way we typically get accurate measurements of events that take less than clock resolution time is using statistics, e.g. by running the operation millions of times then dividing the time taken by a million. More advanced statistics are required when you can't simply run the operation in a loop, e.g. if you are perfing "execute trade on stock market", in which case you can still collect millions of samples of adjacent clock values just not in a loop and a proportion of them happened to be between counter increments, from which you can estimate the real time taken.
4 күн бұрын
For a simple case like this, I think I would use "context = open(file) if file else contextlib.nullcontext(sys.stdin)"
@ArielVolovik4 күн бұрын
would this properly close the file?
4 күн бұрын
@ArielVolovik Yes, but there's a chance the process terminates between the `open` and the place where you use the context
@prosodyspeaks40364 күн бұрын
how about file, cm = (file, open) if file else (sys.stdin, nullcontext) with cm(file) as stream: return next(iter(stream)) handles the cases anthony uses in the vid
@PanduPoluanКүн бұрын
@@prosodyspeaks4036What if you want to specify parameters to the open() call?
@rednafi4 күн бұрын
ExitStack is magic. I'm yet to encounter something so elegant in any of the few languages that I write alongside Python--TS, Go, Elixir, etc.
@vasiliigulevich92024 күн бұрын
It is RAII concept from C++ and Ada. Python and Java use libraries to achieve the effect.
@QckSGaming4 күн бұрын
Go has context and defer for example. I've never seen a valid use case for ExitStack. Just write proper code in the first place and don't wrap it in some conditionally closed context.
@vasiliigulevich92024 күн бұрын
@QckSGaming it is needed to track out-of-stack lifetimes. For example a log-lived socket connection that can't be closed after method returns.
@QckSGaming4 күн бұрын
@@vasiliigulevich9202 Huh? It runs all exit callbacks when you exit the scope. Are you talking about something else? In any case, that does not sound like a place where you would want to combine several context managers into a single with statement..
@vasiliigulevich92024 күн бұрын
@QckSGaming yes, but the scope is not limited to a method or a with keyword. Asking ExitStack to na member and manage lifetimes of other members or dependencies. I usually implement C++ RAII with it by disposing all members together with their container. But it is a bit more flexible than just that.
@senpos4 күн бұрын
"All Code Sucks" is a lot of fun! One-liners are so tempting. There's always that itch inside to make the solution fancy and perfect and smart, but maybe writing simple and straightforward code is better after all. :D
@jtw-r4 күн бұрын
Yes I agree 100% about one liners. If you find yourself writing a one liner often, abstract it to a function and name it accordingly so it “reads” easier to the other developers. Something as simple as inline sorting logic can be extracted to a named function to make it even clearer (i.e. sort_by_page_title_descending() ) Tangentially, I’m of the camp that the longer the variable names are, the better documented and easier to read your codebase is. Definitely not a 1to1 but it’s always a good sign.
@mrswats4 күн бұрын
That's a dirty bug!
@jake1155775 күн бұрын
This is something I should have learned when I was just starting. Thanks as always, Anthony!
@tylermfdurden5 күн бұрын
when you figure this stuff out, can you come fix my computer? It also has several nonsensical issues that I can't even explain.
@PeterBenjamin-KH6 күн бұрын
Ah, yes. It's a classic problem with the inverse reactive current used in unilateral phase detractors and synchronization of cardinal gram-meters. See, the power used to be generated by the relative motion of conductors and fluxors, but in your VM, it is likely generated by the modial interaction of magneto-reluctance and capacitive deractance. I would check the base plate of prefamulated amulyte that is surmounted by a maleable logarithmic casing and make sure the 2 sperving barings run a direct line with the parametric fan. The Rockwell Retro Encabulator YT video goes into deep detail on this.
@spencer37526 күн бұрын
Would you be willing to change hypervisor? Can you try creating the VM (or converting your disk image to a vhd) with Hyper-V and see if it has the same behavior? Hyper-V is a type-1 hypervisor and will probably give a much better experience overall... not to mention is included with Windows. As a stab in the dark... some of the weirdest issues I've experienced have often been related to a disk partition being full. Double check all your partitions are mapped appropriately and have free space. Dynamically expanding storage can also do weird things because the machine basically stops very briefly when that occurs, which causes weird behavior sometimes.
@thiemofischer22306 күн бұрын
don't cite me on this, but for the time being: just omit the outliers.
@anthonywritescode6 күн бұрын
that seems like a terrible idea. if you know there's time inaccuracy then all of your data is trash
@ColopoptizPlays6 күн бұрын
Someone already mentioned the time stamp counter but maybe try removing and checking the cmos battery?
@thiemofischer22306 күн бұрын
there is no algorithm that can solve --warn-unreachable. it's the halting problem
@anthonywritescode6 күн бұрын
for a strict subset of the language it's fairly straightforward actually (you of course need to make some basic assumptions about various constructs but at least for the subset mypy targets it's not that unreasonable to "solve")
@thiemofischer22305 күн бұрын
@@anthonywritescode maybe. i don't know that much about how mypy would go about it, when just looking at types. take the str vs Sequence[str] example from the other video. make an expression to check if the elements of the Sequence are all one character strings. put that in an "if !expession: pass". use --warn-unreachable on that. This should "solve" for the type exclusion (just in theory). just wondering what assumptions would go in the algorithm... straightforward enough to make my brain melt at least
@MynecraftCZ6 күн бұрын
You can write a simple C program which store time to array in a loop as fast as possible and then plot it to see if there are some stairs, spikes or whatever. It can give you better understanding what is happening. You can also experiment with using clock_realtime and clock_monotonic.
@DDlol016 күн бұрын
manual linux kernel replacement might fix it (just use the same version as installed first, then upgrade to something newer) that fixed it on an arm based system for me (a single core 400 MHz crappy softfp one, but hey, maybe) But that would have been a problem from the first boot. Firmware Update for BIOS/CPU would be better to try (mostly fixes the fixable things above). If its just in specific software idk, reinstall that software and things it depends on? custom build systemd : D (the last one was a joke, systemd is not so fun to replace, or at least it was when it became standard a decade ago) run a live cd and check times from there. If it is messed up still it might be the internal highprecission counter from the cpu it self is somewhat broken/buggy. Barely recoverable depending on the actual cause, CMOS/BIOS reset might fix that, another CPU (a crappy celeron-esque will do) can even help. there are wild suggestions online: Vaporizing the PC might do : D Would not reccomend checking that. Took me days on a laptop to figure this one out. And I could not fix it. My PC turned so slow it got to 50MHz tops for some reasons and I gave up. Usually this means your CPU is slowly turning insane. I am just taking wild guesses...
@YonatanAvhar7 күн бұрын
I'd try booting a live USB to see if it happens bear metal Additionally, I'd try a different hypervisor like VMWare or MS Hyper-V
@LewisCowles7 күн бұрын
I mean... First try it outside of a VM, and then try a different OS (windows can run in trial mode for 30-90 days)
@TheShadow063207 күн бұрын
Looks like KZbin deleted my original comment (likely due to it containing the commands I used), so I am commenting again. FWIW, I cannot reproduce the 0 ns thing. My CPU is Ryzen 3900X. I tried to reproduce it in a VM (Virtualbox with the same CPU settings; with Ubuntu 18.04, Lubuntu 20.04, Ubuntu 24.04 and PopOS 22.04) and on bare metal (Debian 12). Hope this data point would be useful in figuring it out. If I had to guess the source of the problem, it would be in the part of either the NT kernel or the firmware, which is responsible for reading the real-time clock. You could try to narrow it down by testing on the host, while it is running Linux (a live usb should suffice).
@nevo4087 күн бұрын
Not sure exactly how `time` is implemented under the hood. I would try another language to see if this behavior is reproducible across different implementations
@anthonywritescode7 күн бұрын
I'm not actually using `time` but as I've replied in a few other comments this isn't specific to python
@cs2337 күн бұрын
Wrong approach! Figure out how it dies things in zero nanoseconds and leave those fancy quantum machines in the dust! You’ll make a fortune!
@senyai7 күн бұрын
Could it be that the process is migrated to different CCD with different timer?
@antonboch12447 күн бұрын
AMD issue... I think.
@antonboch12447 күн бұрын
It's also can be an execution cache.
@noahcuroe8 күн бұрын
This feels like either a hypervisor and/or hardware virtualization bug or an intentional decision. Since you are on Windows I would try running a tool called InSpectre and disabling side channel mitigations there. If that alone doesn't work I'd comb through your host BIOS settings and Virtualbox settings for anything related to performance counters, hardware timers, or side channel attack mitigations and start flipping switches and checking. My (kind of a stretch) theory is that the system is adding noise to system time counters to mitigate side channel attacks - this was notably employed in response to Meltdown and Spectre but there have been other side channel attacks discovered since then that may have warranted heavier handed mitigations especially in virtualization. The implementation of this mitigation will be different on Intel and AMD which could be why you can't reproduce this on Intel.
@SashikaSandeepa8 күн бұрын
just dropped another X files episode 😅
@bluekeybo8 күн бұрын
TempleOS never had this issue, just saying
@anthonywritescode8 күн бұрын
I'm nearly certain templeos will have the same behaviour -- will try it when I can figure out how to make a vm of it
@wenbozhao43258 күн бұрын
What if change the best-of script to a bash loop so it's at least not related to python at all and see if it's reproduced
@anthonywritescode8 күн бұрын
it reproduces with C so it's definitely not python at fault
@MelroyvandenBerg8 күн бұрын
Yeah try set the vm to use directly the host cpu. And try also kvm.
@anthonywritescode8 күн бұрын
what does that mean?
@MelroyvandenBerg7 күн бұрын
@@anthonywritescode Well there are different hypervisors like Promox for example that uses QEMU/KVM kernel modules. You could check if for example Qemu is getting different results. I also notice a Virtualbox setting at: System -> Acceleration -> Para-virtualization interface to select KVM instead. Regarding the CPU type as host, I'm not fully sure if Virtualbox has this same feature as Proxmox (that is using KVM). I do can execute: VBoxManage list vms. And set the cpu profile host: VBoxManage modifyvm "{some-hex-value}" --cpu-profile host. But this seems already be the default. And again, maybe not the same as KVM CPU host type (which will not virtualize the CPU to the VM).
@anthonywritescode7 күн бұрын
kvm seems to be the default for Linux guests
@that_guy_06998 күн бұрын
I know exactly what the problem is! Is what I would say, if I would know what the problem is. However, I have no idea.
@anthonywritescode8 күн бұрын
genius!
@sethdhanson8 күн бұрын
Is it plugged in?
@anthonywritescode8 күн бұрын
ah shoot ya got me there
@leggysoft8 күн бұрын
I've seen failing clocks give the same value more than once so time is not stopping it's just repeating the time. This can cause drift, if no drift it's being stupid but the clock itself is working.
@anthonywritescode8 күн бұрын
what
@adminisnyator8 күн бұрын
Really hope this is not going to be one of those weird Intel issues
@anthonywritescode8 күн бұрын
see that's the thing it's only broken on amd :'(
@Person18738 күн бұрын
Have you tried under different hypervisors? I believe virtualbox does some amount of CPU result caching which could potentially explain this.
@anthonywritescode8 күн бұрын
I haven't but vbox is working fine everywhere else with identical settings
@anathaetownsend18948 күн бұрын
@@anthonywritescode There may be an interaction between the Virtual Box hypervisor and the AMD processor. If HyperV, or VMWare Workstation Pro reproduce the problem on that hardware, then hardware is involved in some way, if you can't replicate under a different hypervisor, then there is an edge condition that virtual box exposes somehow that the others do not. If you can get a chance to try with a live USB using Linux and KVM.
@berndeckenfels8 күн бұрын
Did you try to pin the cores to a single physical core (or package) or numa zone? What’s the host? I think perfcountere are per cpu - so is that a dual socket?
@anthonywritescode8 күн бұрын
how would one do that? host is windows and it's a single cpu -- ryzen 9 7950x
@xspager8 күн бұрын
Maybe try disabling the "Extended features"
@xspager8 күн бұрын
Also you could try to change System > Acceleration > Paravirtualization interface to Legacy or Minimal and see if it changes anything.
@anthonywritescode8 күн бұрын
hmmm you might be on to something -- will try when I am back from the wilderness
@K9Megahertz8 күн бұрын
It's cause you have that malware python installed. =)
@anthonywritescode8 күн бұрын
it reproduces in the holy C as well
@K9Megahertz8 күн бұрын
@@anthonywritescode Weird issue for sure, yeah I thought it might just be some python shenanigans but if C is doing it as well then I feel it's definitely in the virtualization layer. Does adding a delay in between the time calls prevent the issue from happening? I wouldn't use sleep but maybe just eat up some cycles, or hell, just try sleep as well maybe with 1ms.
@anthonywritescode7 күн бұрын
it's running a subprocess in between (literally thousands and thousands of instructions)
@K9Megahertz7 күн бұрын
@@anthonywritescode Oh, I do see that, I was just curious how far you could push that. Would you still get 0.000 with a 5 second delay in between?
@K9Megahertz7 күн бұрын
@@anthonywritescode I posted a reply to this but I guess YT censored it.... No, I understand you have something there taking up cycles. I guess I was suggesting to increase the time between the calls. What if you add a 5 second delay? Still 0s?
@hambroman8 күн бұрын
Does it happen on a newly created VM? No idea if this is even a thing, but wonder if there's something host-specific baked in from the prior machine you built this image on?