-
Notifications
You must be signed in to change notification settings - Fork 234
Multithreading
Andy Stewart edited this page Jan 9, 2022
·
4 revisions
I briefly sorted out how EAF implements the multi-threaded programming model. For the convenience of practice, it is recommended to refer to the GitCommitThread
code implementation in EAF file-manager buffer.py:
- The principle of QThread is very simple, first create a QThread class, and then throw the time-consuming code into the
run
function - Create an instance of QThread in the main thread (EAF is the function of AppBuffer). In order to avoid calling the function scope out of the QThread instance and causing the QThread instance to be destroyed, you can add the QThread instance to a queue object to keep the reference (refer to the
fetch_git_log_threads
of EAF file-manager). - After the QThread is created, calling thread.start() directly will run the time-consuming code in a child thread
- In graphical programming, the principle of "sub-threads cannot directly operate the GUI code, only the main thread can operate the GUI code, after the run function in QThread completes the time-consuming operation, it needs to send a signal to the main thread to remind the main thread to refresh the GUI (refer to
fetch_command_result
signal ofGitCommitThread
) - The main thread receives the completion signal of QThread and calls the GUI code to refresh the interface. In order to comply with the principle in step 4, the callback function for refreshing the GUI needs to be wrapped with the decorator @PostGui() to ensure that the operation GUI code only runs in the main thread ( For details, refer to the
update_git_log function
of EAF file-manager)
The above are all the key steps to implement multithreading in EAF, and this principle also applies to any Qt program.