|
Original |
Translation |
|
997
|
import threading, zipfile class AsyncZip(threading.Thread): def __init__(self, infile, outfile): threading.Thread.__init__(self) self.infile = infile self.outfile = outfile def run(self): f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED) f.write(self.infile) f.close() print('Finished background zip of:', self.infile) background = AsyncZip('mydata.txt', 'myarchive.zip') background.start() print('The main program continues to run in foreground.') background.join() # Wait for the background task to finish print('Main program waited until background was done.')
|
|
|
998
|
The principal challenge of multi-threaded applications is coordinating threads that share data or other resources. To that end, the threading module provides a number of synchronization primitives including locks, events, condition variables, and semaphores.
|
|
|
999
|
While those tools are powerful, minor design errors can result in problems that are difficult to reproduce. So, the preferred approach to task coordination is to concentrate all access to a resource in a single thread and then use the :mod:`queue` module to feed that thread with requests from other threads. Applications using :class:`Queue` objects for inter-thread communication and coordination are easier to design, more readable, and more reliable.
|
|
|
1000
|
|
|
|
1001
|
|
1002
|
import logging logging.debug('Debugging information') logging.info('Informational message') logging.warning('Warning:config file %s not found', 'server.conf') logging.error('Error occurred') logging.critical('Critical error -- shutting down')
|
|
|
1003
|
This produces the following output::
|
|
|
1004
|
WARNING:root:Warning:config file server.conf not found ERROR:root:Error occurred CRITICAL:root:Critical error -- shutting down
|
|
|
1005
|
By default, informational and debugging messages are suppressed and the output is sent to standard error. Other output options include routing messages through email, datagrams, sockets, or to an HTTP Server. New filters can select different routing based on message priority: :const:`DEBUG`, :const:`INFO`, :const:`WARNING`, :const:`ERROR`, and :const:`CRITICAL`.
|
|
|
1006
|
The logging system can be configured directly from Python or can be loaded from a user editable configuration file for customized logging without altering the application.
|
|