Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/library/threading.rst

    r2 r391  
    55   :synopsis: Higher-level threading interface.
    66
     7**Source code:** :source:`Lib/threading.py`
     8
     9--------------
    710
    811This module constructs higher-level threading interfaces on top of the  lower
     
    1518.. note::
    1619
    17    Starting with Python 2.6, this module provides PEP 8 compliant aliases and
     20   Starting with Python 2.6, this module provides :pep:`8` compliant aliases and
    1821   properties to replace the ``camelCase`` names that were inspired by Java's
    1922   threading API. This updated API is compatible with that of the
     
    2730   instead of :exc:`AssertionError` if called erroneously.
    2831
     32.. impl-detail::
     33
     34   In CPython, due to the :term:`Global Interpreter Lock`, only one thread
     35   can execute Python code at once (even though certain performance-oriented
     36   libraries might overcome this limitation).
     37   If you want your application to make better use of the computational
     38   resources of multi-core machines, you are advised to use
     39   :mod:`multiprocessing`. However, threading is still an appropriate model
     40   if you want to run multiple I/O-bound tasks simultaneously.
     41
    2942
    3043This module defines the following functions and objects:
     
    3548   Return the number of :class:`Thread` objects currently alive.  The returned
    3649   count is equal to the length of the list returned by :func:`.enumerate`.
     50
     51   .. versionchanged:: 2.6
     52      Added ``active_count()`` spelling.
    3753
    3854
     
    4359   variable allows one or more threads to wait until they are notified by another
    4460   thread.
     61
     62   See :ref:`condition-objects`.
    4563
    4664
     
    5371   returned.
    5472
     73   .. versionchanged:: 2.6
     74      Added ``current_thread()`` spelling.
     75
    5576
    5677.. function:: enumerate()
     
    7091   is true.
    7192
     93   See :ref:`event-objects`.
     94
    7295
    7396.. class:: local
     
    93116   acquired it, subsequent attempts to acquire it block, until it is released; any
    94117   thread may release it.
     118
     119   See :ref:`lock-objects`.
    95120
    96121
     
    101126   reentrant lock, the same thread may acquire it again without blocking; the
    102127   thread must release it once for each time it has acquired it.
     128
     129   See :ref:`rlock-objects`.
    103130
    104131
     
    112139   given, *value* defaults to 1.
    113140
     141   See :ref:`semaphore-objects`.
     142
    114143
    115144.. function:: BoundedSemaphore([value])
     
    123152
    124153.. class:: Thread
     154   :noindex:
    125155
    126156   A class that represents a thread of control.  This class can be safely
    127157   subclassed in a limited fashion.
    128158
     159   See :ref:`thread-objects`.
     160
    129161
    130162.. class:: Timer
     163   :noindex:
    131164
    132165   A thread that executes a function after a specified interval has passed.
     166
     167   See :ref:`timer-objects`.
    133168
    134169
     
    139174   Set a trace function for all threads started from the :mod:`threading` module.
    140175   The *func* will be passed to  :func:`sys.settrace` for each thread, before its
    141    :meth:`run` method is called.
     176   :meth:`~Thread.run` method is called.
    142177
    143178   .. versionadded:: 2.3
     
    150185   Set a profile function for all threads started from the :mod:`threading` module.
    151186   The *func* will be passed to  :func:`sys.setprofile` for each thread, before its
    152    :meth:`run` method is called.
     187   :meth:`~Thread.run` method is called.
    153188
    154189   .. versionadded:: 2.3
     
    174209   .. versionadded:: 2.5
    175210
     211
     212.. exception:: ThreadError
     213
     214   Raised for various threading-related errors as described below.  Note that
     215   many interfaces use :exc:`RuntimeError` instead of :exc:`ThreadError`.
     216
     217
    176218Detailed interfaces for the objects are documented below.
    177219
     
    219261through the :attr:`daemon` property.
    220262
     263.. note::
     264   Daemon threads are abruptly stopped at shutdown.  Their resources (such
     265   as open files, database transactions, etc.) may not be released properly.
     266   If you want your threads to stop gracefully, make them non-daemonic and
     267   use a suitable signalling mechanism such as an :class:`Event`.
     268
    221269There is a "main thread" object; this corresponds to the initial thread of
    222270control in the Python program.  It is not a daemon thread.
     
    260308      object's :meth:`run` method to be invoked in a separate thread of control.
    261309
    262       This method will raise a :exc:`RuntimeException` if called more than once
     310      This method will raise a :exc:`RuntimeError` if called more than once
    263311      on the same thread object.
    264312
     
    294342      raises the same exception.
    295343
    296    .. method:: getName()
    297                setName()
    298 
    299       Old API for :attr:`~Thread.name`.
    300 
    301344   .. attribute:: name
    302345
     
    304347      Multiple threads may be given the same name.  The initial name is set by
    305348      the constructor.
     349
     350      .. versionadded:: 2.6
     351
     352   .. method:: getName()
     353               setName()
     354
     355      Pre-2.6 API for :attr:`~Thread.name`.
    306356
    307357   .. attribute:: ident
     
    320370      Return whether the thread is alive.
    321371
    322       Roughly, a thread is alive from the moment the :meth:`start` method
    323       returns until its :meth:`run` method terminates. The module function
     372      This method returns ``True`` just before the :meth:`run` method starts
     373      until just after the :meth:`run` method terminates. The module function
    324374      :func:`.enumerate` returns a list of all alive threads.
    325375
    326    .. method:: isDaemon()
    327                setDaemon()
    328 
    329       Old API for :attr:`~Thread.daemon`.
     376      .. versionchanged:: 2.6
     377         Added ``is_alive()`` spelling.
    330378
    331379   .. attribute:: daemon
     
    339387
    340388      The entire Python program exits when no alive non-daemon threads are left.
     389
     390      .. versionadded:: 2.6
     391
     392   .. method:: isDaemon()
     393               setDaemon()
     394
     395      Pre-2.6 API for :attr:`~Thread.daemon`.
    341396
    342397
     
    359414:meth:`release` method should only be called in the locked state; it changes the
    360415state to unlocked and returns immediately. If an attempt is made to release an
    361 unlocked lock, a :exc:`RuntimeError` will be raised.
     416unlocked lock, a :exc:`ThreadError` will be raised.
    362417
    363418When more than one thread is blocked in :meth:`acquire` waiting for the state to
     
    369424
    370425
    371 .. method:: Lock.acquire([blocking=1])
     426.. method:: Lock.acquire([blocking])
    372427
    373428   Acquire a lock, blocking or non-blocking.
    374429
    375    When invoked without arguments, block until the lock is unlocked, then set it to
    376    locked, and return true.
    377 
    378    When invoked with the *blocking* argument set to true, do the same thing as when
    379    called without arguments, and return true.
    380 
    381    When invoked with the *blocking* argument set to false, do not block.  If a call
    382    without an argument would block, return false immediately; otherwise, do the
    383    same thing as when called without arguments, and return true.
     430   When invoked with the *blocking* argument set to ``True`` (the default),
     431   block until the lock is unlocked, then set it to locked and return ``True``.
     432
     433   When invoked with the *blocking* argument set to ``False``, do not block.
     434   If a call with *blocking* set to ``True`` would block, return ``False``
     435   immediately; otherwise, set the lock to locked and return ``True``.
    384436
    385437
     
    392444   to proceed.
    393445
    394    Do not call this method when the lock is unlocked.
     446   When invoked on an unlocked lock, a :exc:`ThreadError` is raised.
    395447
    396448   There is no return value.
     
    545597      reacquired.
    546598
    547    .. method:: notify()
    548 
    549       Wake up a thread waiting on this condition, if any.  If the calling thread
    550       has not acquired the lock when this method is called, a
     599   .. method:: notify(n=1)
     600
     601      By default, wake up one thread waiting on this condition, if any.  If the
     602      calling thread has not acquired the lock when this method is called, a
    551603      :exc:`RuntimeError` is raised.
    552604
    553       This method wakes up one of the threads waiting for the condition
    554       variable, if any are waiting; it is a no-op if no threads are waiting.
    555 
    556       The current implementation wakes up exactly one thread, if any are
    557       waiting.  However, it's not safe to rely on this behavior.  A future,
    558       optimized implementation may occasionally wake up more than one thread.
    559 
    560       Note: the awakened thread does not actually return from its :meth:`wait`
     605      This method wakes up at most *n* of the threads waiting for the condition
     606      variable; it is a no-op if no threads are waiting.
     607
     608      The current implementation wakes up exactly *n* threads, if at least *n*
     609      threads are waiting.  However, it's not safe to rely on this behavior.
     610      A future, optimized implementation may occasionally wake up more than
     611      *n* threads.
     612
     613      Note: an awakened thread does not actually return from its :meth:`wait`
    561614      call until it can reacquire the lock.  Since :meth:`notify` does not
    562615      release the lock, its caller should.
     
    569622      calling thread has not acquired the lock when this method is called, a
    570623      :exc:`RuntimeError` is raised.
     624
     625      .. versionchanged:: 2.6
     626         Added ``notify_all()`` spelling.
    571627
    572628
     
    625681
    626682Semaphores are often used to guard resources with limited capacity, for example,
    627 a database server.  In any situation where the size of the resource size is
    628 fixed, you should use a bounded semaphore.  Before spawning any worker threads,
    629 your main thread would initialize the semaphore::
     683a database server.  In any situation where the size of the resource is fixed,
     684you should use a bounded semaphore.  Before spawning any worker threads, your
     685main thread would initialize the semaphore::
    630686
    631687   maxconnections = 5
     
    668724      Return true if and only if the internal flag is true.
    669725
     726      .. versionchanged:: 2.6
     727         Added ``is_set()`` spelling.
     728
    670729   .. method:: set()
    671730
     
    707766and as such also functions as an example of creating custom threads.
    708767
    709 Timers are started, as with threads, by calling their :meth:`start` method.  The
    710 timer can be stopped (before its action has begun) by calling the :meth:`cancel`
    711 method.  The interval the timer will wait before executing its action may not be
    712 exactly the same as the interval specified by the user.
     768Timers are started, as with threads, by calling their :meth:`~Timer.start`
     769method.  The timer can be stopped (before its action has begun) by calling the
     770:meth:`~Timer.cancel` method.  The interval the timer will wait before
     771executing its action may not be exactly the same as the interval specified by
     772the user.
    713773
    714774For example::
     
    759819--------------------------
    760820
    761 While the import machinery is thread safe, there are two key
    762 restrictions on threaded imports due to inherent limitations in the way
    763 that thread safety is provided:
     821While the import machinery is thread-safe, there are two key restrictions on
     822threaded imports due to inherent limitations in the way that thread-safety is
     823provided:
    764824
    765825* Firstly, other than in the main module, an import should not have the
Note: See TracChangeset for help on using the changeset viewer.