Hello!
I've been building my own engine in python (I know c++ is better but I chose differently) and created two threads - one is worker and does the calculation, one watchdog and waits for input. I've done the same thing in C++ for comparison and they behave identically - I cannot pass info strings with my calculation results from worker until watchdog stops blocking the channel waiting for input. Once it stops (gets command from GUI...) then all the info from worker is printed at once.
I did my best to research non-blocking input functions in python but after I wrote the same thing in C++ and it also did not work, I ralized it might not be language problem but approach problem. Can you please hint me how this task is usually handeled? My solutions do not seem to work...
Thank you very much!
Blocking input
Moderators: hgm, Rebel, chrisw
-
- Posts: 12
- Joined: Wed Jul 05, 2017 12:44 pm
- Location: Czech Republic
Blocking input
Best regards,
Miloš
Miloš
-
- Posts: 570
- Joined: Mon Jul 20, 2015 5:06 pm
Re: Blocking input
Your second watchdog thread might also be refered to as a polling thread. Do not try to pass strings through the polling thread. This would require a mutex. Instead, set a flag inside the polling thread to indicate some information is available. Then, print your information when the main thread is ready.maelic wrote: ↑Fri Nov 02, 2018 4:56 pm Hello!
I've been building my own engine in python (I know c++ is better but I chose differently) and created two threads - one is worker and does the calculation, one watchdog and waits for input. I've done the same thing in C++ for comparison and they behave identically - I cannot pass info strings with my calculation results from worker until watchdog stops blocking the channel waiting for input. Once it stops (gets command from GUI...) then all the info from worker is printed at once.
I did my best to research non-blocking input functions in python but after I wrote the same thing in C++ and it also did not work, I ralized it might not be language problem but approach problem. Can you please hint me how this task is usually handeled? My solutions do not seem to work...
Thank you very much!
-
- Posts: 892
- Joined: Sun Nov 19, 2006 9:16 pm
- Location: Russia
Re: Blocking input
You need to do std::flush() after printing each info string.
-
- Posts: 12
- Joined: Wed Jul 05, 2017 12:44 pm
- Location: Czech Republic
Re: Blocking input
Such an elegant solution!! I cannot believe I have not found any mentions of it during my 12 days research on non-blocking input...Aleks Peshkov wrote: ↑Fri Nov 02, 2018 5:27 pm You need to do std::flush() after printing each info string.
Thank you very much
Best regards,
Miloš
Miloš