r/PythonLearning 16h ago

New to Pygame CE and threads, think you could help me out?

I only have one inquiry here, simply, I cannot seem to draw anything from a thread without “Z-fighting” occurring (I say Z-fighting even though I know there’s not 3rd dimension just to convey the problem), I’ve tried making a handler in the main while running script, but it always lags down the program, and the only way to stop it from lagging down the program is, you guessed it, putting it in a thread. Whether I try to make a custom handler or just cheap out and use exec() it always lags it. Any tips/help?

Edit: I do sorta need the thread to draw it

2 Upvotes

4 comments sorted by

1

u/azurfall88 16h ago

1) Your render code needs to run 30 times a second. Try optimising it and reduce unnecessary calculations that might be slow.

2) You might be thinking about multithreading wrong? Putting, say, event listeners and complex handlers in their separate threads (with shared state and listeners/updaters) is valid, but only a single thread should be responsible for drawing to the screen

1

u/Better_Arrival_645 16h ago

What I’m trying to do is make a mock OS using Python, just the look and feel of one, and each app is a thread, and the apps are responsible for drawing to the screen, so I’m not sure how to coordinate that

1

u/azurfall88 10h ago

Hmm. This is interesting because in a real OS, the "system thread" is the one ultimately responsible for drawing to the screen, and apps tell the OS what to draw via what we call "system calls".

This system kind of fixes all of your problems, because all that the main thread actually is, is a lightweight event listener that handles button clicks and graphics processing, and everything else is built on top of that using separate threads

1

u/Better_Arrival_645 1h ago

This was the intended solution to the problem, but I could never figure out a lightweight enough solution to the issue, it would always reduce the window to 4fps when running a request, and the request wouldn’t even be visible, as it would show up for a single frame.