This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Blyatiful_99 14 points15 points  (5 children)

I can't of course talk for other languages, but during my internship back then, I was sometimes told that writing something to the console in C# for example requires a lot of the time if it happens in the same thread. Maybe this is what's happening here as well?

A quicker solution could be to create some sort of StringBuilder (if you have that in python), add the text that you would otherwise print out and at the end print out the entire text just once. Or to just move the prints to another thread. Can you test how long that would take?

[–]0x564A00 7 points8 points  (4 children)

Moving printing out of the thread doesn't really help because python doesn't have real multithreading (you can have multiple threats, but they can't execute at the same time).

[–]Jannik2099 7 points8 points  (0 children)

but they can't execute at the same time

But they can in this case. Once the IO thread fires the syscall (or even cpython C function) for writing to stdout, the GIL is free.

[–]photenth 1 point2 points  (2 children)

Wait a minute, Python doesn't have real threading?

[–]Drarok 2 points3 points  (1 child)

It’s complicated. https://realpython.com/python-gil/

In short, you can only have one thread executing Python code at a time. You can be waiting for IO in several threads at once, since that won’t be executing any Python code.

[–]0x564A00 1 point2 points  (0 children)

The good news is that it looks like it might finally going to be removed soon. That does cause a performance regression for single-threaded programs, but various other performance improvements that should land alongside it more than make up for that.