@@ -4868,12 +4868,12 @@ \section{Working with Binary Data Record Layouts\label{binary-formats}}
48684868\section {Multi-threading\label {multi-threading } }
48694869
48704870Threading is a technique for decoupling tasks which are not sequentially
4871- dependent. Python threads are driven by the operating system and run
4872- in a single process and share memory space in a single interpreter.
4871+ dependent. Threads can be used to improve the responsiveness of
4872+ applications that accept user input while other tasks run in the
4873+ background. A related use case is running I/O in parallel with
4874+ computations in another thread.
48734875
4874- Threads can be used to improve the responsiveness of applications that
4875- accept user input while other tasks run in the background. The
4876- following code shows how the high level
4876+ The following code shows how the high level
48774877\ulink {\module {threading}}{../lib/module-threading.html} module can run
48784878tasks in background while the main program continues to run:
48794879
@@ -4891,8 +4891,12 @@ \section{Multi-threading\label{multi-threading}}
48914891 f.close()
48924892 print 'Finished background zip of: ', self.infile
48934893
4894- AsyncZip('mydata.txt', 'myarchive.zip').start()
4895- print 'The main program continues to run'
4894+ background = AsyncZip('mydata.txt', 'myarchive.zip')
4895+ background.start()
4896+ print 'The main program continues to run in foreground.'
4897+
4898+ background.join() # Wait for the background task to finish
4899+ print 'Main program waited until background was done.'
48964900\end {verbatim }
48974901
48984902The principal challenge of multi-threaded applications is coordinating
@@ -4901,13 +4905,13 @@ \section{Multi-threading\label{multi-threading}}
49014905events, condition variables, and semaphores.
49024906
49034907While those tools are powerful, minor design errors can result in
4904- problems that are difficult to reproduce. A simpler and more robust
4905- approach to task coordination is concentrating all access to a resource
4908+ problems that are difficult to reproduce. So, the preferred approach
4909+ to task coordination is to concentrate all access to a resource
49064910in a single thread and then using the
49074911\ulink {\module {Queue}}{../lib/module-Queue.html} module to feed that
4908- thread with requests from other threads. Applications that use
4912+ thread with requests from other threads. Applications using
49094913\class {Queue} objects for inter-thread communication and coordination
4910- tend to be easier to design, more readable, and more reliable.
4914+ are easier to design, more readable, and more reliable.
49114915
49124916
49134917\section {Logging\label {logging } }
0 commit comments