
Other Built-in Modules
|
129
The datetime Module
Tools for subtracting dates, adding days to dates, and so on.
See the Python Library Manual for details.
from datetime import date, timedelta
>>> date(2004, 12, 17) - date(2004, 11, 29)
datetime.timedelta(18)
>>> date(2004, 11, 29) + timedelta(18)
datetime.date(2004, 12, 17)
Threading Modules
Threads are lightweight processes that share global memory
(i.e., lexical scopes and interpreter internals) and all run in
parallel within the same process. Python thread modules
work portably across platforms.
thread
Python’s basic thread interface module. Tools to start,
stop, and synchronize functions run in parallel. To
spawn a thread:
thread.start_new_thread(function,
argstuple)
. Function start_new is a synonym for start_
new_thread
. To synchronize threads, use thread locks:
lock=thread.allocate_lock(); lock.acquire(); update-
objects
; lock.release().
threading
Module threading builds upon thread, to provide thread-
ing-oriented classes:
Thread, Condition, Semaphore, Lock,
etc. Subclass
Thread to overload run action method.
Queue
A multiproducer, multiconsumer FIFO queue of objects
implementation, especially useful for threaded applica-
tions (see the Python Library Reference). Locks
get and
put operations to synchronize access to data on the
queue.