Commit Graph

67 Commits

Author SHA1 Message Date
Jeffrey Yasskin 69e1309fd4 Thread.start() used sleep(0.000001) to make sure it didn't return before the
new thread had started. At least on my MacBook Pro, that wound up sleeping for
a full 10ms (probably 1 jiffy). By using an Event instead, we can be absolutely
certain that the thread has started, and return more quickly (217us).

Before:
$  ./python.exe -m timeit -s 'from threading import Thread'  't = Thread(); t.start(); t.join()'
100 loops, best of 3: 10.3 msec per loop
$  ./python.exe -m timeit -s 'from threading import Thread; t = Thread()'  't.isAlive()'
1000000 loops, best of 3: 0.47 usec per loop

After:
$  ./python.exe -m timeit -s 'from threading import Thread'  't = Thread(); t.start(); t.join()'
1000 loops, best of 3: 217 usec per loop
$  ./python.exe -m timeit -s 'from threading import Thread; t = Thread()'  't.isAlive()'
1000000 loops, best of 3: 0.86 usec per loop

To be fair, the 10ms isn't CPU time, and other threads including the spawned
one get to run during it. There are also some slightly more complicated ways to
get back the .4us in isAlive() if we want.
2008-02-28 06:09:19 +00:00
Jeffrey Yasskin a885c1521a Followup to r61011: Also avoid the reference cycle when the Thread's target
raises an exception.
2008-02-23 20:40:35 +00:00
Jeffrey Yasskin 3414ea9ed9 Prevent classes like:
class RunSelfFunction(object):
        def __init__(self):
            self.thread = threading.Thread(target=self._run)
            self.thread.start()
        def _run(self):
            pass
from creating a permanent cycle between the object and the thread by having the
Thread delete its references to the object when it completes.

As an example of the effect of this bug, paramiko.Transport inherits from
Thread to avoid it.
2008-02-23 19:40:54 +00:00
Raymond Hettinger 70ec29d0f4 Revert 60189 and restore performance. 2008-01-24 18:12:23 +00:00
Gregory P. Smith 95cd5c0b72 - Fix Issue #1703448: A joined thread could show up in the
threading.enumerate() list after the join() for a brief period until
  it actually exited.
2008-01-22 01:20:42 +00:00
Gregory P. Smith 64c5677de4 Replace spam.acquire() try: ... finally: spam.release() with "with spam:" 2008-01-22 01:12:02 +00:00
Guido van Rossum 54ec61ea6e Add a hack (originally devised in a slightly different form by Thomas Wouters)
to prevent spurious tracebacks when a daemon thread's cleanup happens to wake
up when the world around it has already been destroyed.
2007-08-20 15:18:04 +00:00
Nick Coghlan f8bbaa962f Eliminate RLock race condition reported in SF bug #1764059 2007-07-31 13:38:01 +00:00
Collin Winter 50b79ce8e6 Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O). 2007-06-06 00:17:35 +00:00
Martin v. Löwis 7b7c9d4208 Bug #1566280: Explicitly invoke threading._shutdown from Py_Main,
to avoid relying on atexit.
Will backport to 2.5.
2007-01-04 21:06:12 +00:00
Andrew MacIntyre 9291332de1 Patch #1454481: Make thread stack size runtime tunable.
Heavily revised, comprising revisions:
46640 - original trunk revision (backed out in r46655)
46647 - markup fix (backed out in r46655)
46692:46918 merged from branch aimacintyre-sf1454481

branch tested on buildbots (Windows buildbots had problems
not related to these changes).
2006-06-13 15:04:24 +00:00
Tim Peters 28eeefe566 Revert revisions:
46640 Patch #1454481:  Make thread stack size runtime tunable.
46647 Markup fix

The first is causing many buildbots to fail test runs, and there
are multiple causes with seemingly no immediate prospects for
repairing them.  See python-dev discussion.

Note that a branch can (and should) be created for resolving these
problems, like

svn copy svn+ssh://svn.python.org/python/trunk -r46640 svn+ssh://svn.python.org/python/branches/NEW_BRANCH

followed by merging rev 46647 to the new branch.
2006-06-04 23:52:47 +00:00
Andrew MacIntyre 6539d2d3c7 Patch #1454481: Make thread stack size runtime tunable. 2006-06-04 12:31:09 +00:00
Guido van Rossum da5b701aee Get rid of __context__, per the latest changes to PEP 343 and python-dev
discussion.
There are two places of documentation that still mention __context__:
Doc/lib/libstdtypes.tex -- I wasn't quite sure how to rewrite that without
spending a whole lot of time thinking about it; and whatsnew, which Andrew
usually likes to change himself.
2006-05-02 19:47:52 +00:00
Guido van Rossum 8f56d02309 Implement MvL's improvement on __context__ in Condition;
this can just call __context__ on the underlying lock.
(The same change for Semaphore does *not* work!)
2006-04-25 20:12:45 +00:00
Guido van Rossum f669436189 Um, I thought I'd already checked this in.
Anyway, this is the changes to the with-statement
so that __exit__ must return a true value in order
for a pending exception to be ignored.
The PEP (343) is already updated.
2006-03-10 02:28:35 +00:00
Guido van Rossum 1a5e21e033 Updates to the with-statement:
- New semantics for __exit__() -- it must re-raise the exception
  if type is not None; the with-statement itself doesn't do this.
  (See the updated PEP for motivation.)

- Added context managers to:
  - file
  - thread.LockType
  - threading.{Lock,RLock,Condition,Semaphore,BoundedSemaphore}
  - decimal.Context

- Added contextlib.py, which defines @contextmanager, nested(), closing().

- Unit tests all around; bot no docs yet.
2006-02-28 21:57:43 +00:00
Brett Cannon ad07ff2c77 Prevent threading.Thread.join() from blocking when a previous call raised an
exception (e.g., passing in an illegal argument).

Applies patch #1314396.  Thanks Eric Blossom.
2005-11-23 02:15:50 +00:00
Georg Brandl a4a8b820aa bug [ 1238170 ] threading.Thread uses {} as default argument 2005-07-15 09:13:21 +00:00
Brett Cannon 90cece7f89 Fixed typo in verbose output.
Closes bug #1110998.  Thanks Matthew Bogosian.
2005-01-27 22:48:30 +00:00
Tim Peters 711906e0c2 threading._DummyThread.__init__(): document obscure new code.
test_threading.test_foreign_thread():  new test does a basic check that
"foreign" threads can using the threading module, and that they create
a _DummyThread instance in at least one use case.  This isn't a very
good test, since a thread created by thread.start_new_thread() isn't
particularly "foreign".
2005-01-08 07:30:42 +00:00
Brett Cannon e6539c4401 In _DummyThread objects the lock stored in __block (allocated thanks to
_Thread.__init__) was never used.  This is a waste since locks use OS
primitives that are in limited supply.  So the lock is deleted in
_DummyThread.__init__ .

Closes bug #1089632.
2005-01-08 02:43:53 +00:00
Tim Peters 21429932e4 Thread.__delete: Discussion of internal obscurities belongs in comments
rather than in docstrings.  Rewrote so that _active_limbo_lock is released
no matter what happens (it could have been left locked if _sys got None'd
out).  Use "in" in preference to has_key() for dict lookup.  Don't bother
looking for 'dummy_threading' in sys.modules unless KeyError is raised.
Since the heart of the method is the del, do that in only one place.
2004-07-21 03:36:52 +00:00
Brett Cannon 8b3d92a977 Fix bug where a KeyError was raised if -O was being used for the interpreter
and Thread.__delete() was called after a Thread instance was created.  Problem
resulted from a currentThread() call in an 'assert' statement being optimized
out and dummy_thread.get_ident() always returning -1 and thus overwriting the
entry for the _MainThread() instance created in 'threading' at import time.

Closes bug #993394.
2004-07-21 02:21:58 +00:00
Jim Fulton d15dc06df0 Implemented thread-local data as proposed on python-dev:
http://mail.python.org/pipermail/python-dev/2004-June/045785.html
2004-07-14 19:11:50 +00:00
Brett Cannon cc4e935ea5 threading.Thread objects will now print a traceback for an exception raised
during interpreter shutdown instead of masking it with another traceback about
accessing a NoneType when trying to print the exception out in the first place.

Closes bug #754449 (using patch #954922).
2004-07-03 03:52:35 +00:00
Brett Cannon 4b6b7f1515 Remove calls to currentThread() in _Condition methods that were side-effect.
Side-effects were deemed unnecessary and were causing problems at shutdown
time when threads were catching exceptions at start time and then triggering
exceptions trying to call currentThread() after gc'ed.  Masked the initial
exception which was deemed bad.

Fixes bug #754449 .
2004-03-08 22:18:57 +00:00
Raymond Hettinger 756b3f3c15 * Move collections.deque() in from the sandbox
* Add unittests, newsitem, and whatsnew
* Apply to Queue.py mutex.py threading.py pydoc.py and shlex.py
* Docs are forthcoming
2004-01-29 06:37:52 +00:00
Neil Schemenauer f607fc5395 Add traceback.format_exc(). 2003-11-05 23:03:00 +00:00
Tim Peters 59aba128a5 Make the classes exposed by threading.py new-style classes. This is
mostly for convenience and to aid debugging.
2003-07-01 20:01:55 +00:00
Tim Peters 0939fac795 Resolved minor XXX question in the obvious way. 2003-07-01 19:28:44 +00:00
Tim Peters d1b108b953 Whitespace normalization. 2003-06-29 17:24:17 +00:00
Jeremy Hylton 89392c0005 Remove stub settrace() and setprofile() calls. 2003-06-29 17:07:46 +00:00
Jeremy Hylton bfccb35b58 Add settrace() and setprofile() functions to the threading library. 2003-06-29 16:58:41 +00:00
Tim Peters 685e69739e Provide dummy (do-nothing) settrace() and setprofile() functions until
Jeremy can check in the real things.
2003-06-29 16:50:06 +00:00
Guido van Rossum 68468eba63 Get rid of many apply() calls. 2003-02-27 20:14:51 +00:00
Fred Drake a872595f31 - prefer "import ... as" to "import / (assignments) / del" for most things
- when the thread module isn't available, subsequent attempts to import
  threading should not suceed
2002-12-30 23:32:50 +00:00
Guido van Rossum c262a1f51c Add __all__. (Brett Cannon.) 2002-12-30 21:59:55 +00:00
Guido van Rossum 21b60147e9 The _Event class should be more careful with releasing its lock when
interrupted.  A try/finally will do nicely.  Maybe other classes need
this too, but since they manipulate more state it's less clear that
that is always the right thing, and I'm in a hurry.

Backport candidate.
2002-11-21 21:08:39 +00:00
Jeremy Hylton 92bb6e7b96 Docstring nits: The module is neither proposed nor new. 2002-08-14 19:25:42 +00:00
Jeremy Hylton 29c2106465 Explain use of currentThread() in _Condition methods. 2002-08-14 17:56:13 +00:00
Jeremy Hylton 39c12bfba1 Explain a little more. 2002-08-14 17:46:40 +00:00
Jeremy Hylton af7fde7f34 Explain a minor mystery. 2002-08-14 17:43:59 +00:00
Raymond Hettinger 46ac8eb3c8 Code modernization. Replace v=s[i]; del s[i] with single lookup v=s.pop(i) 2002-06-30 03:39:14 +00:00
Guido van Rossum 8ca162f417 Partial introduction of bools where appropriate. 2002-04-07 06:36:23 +00:00
Tim Peters bc0e910826 Convert a pile of obvious "yes/no" functions to return bool. 2002-04-04 22:55:58 +00:00
Neal Norwitz 45bec8c7fc SF #515023. Make _DummyThread.join() signature match base class (Thread) 2002-02-19 03:01:36 +00:00
Guido van Rossum f21b2aafa9 Thread.__bootstrap(): ignore exceptions in the self.__delete() call in
the finally clause.  An exception here could happen when a daemon
thread exits after the threading module has already been trashed by
the import finalization, and there's not much of a point in trying to
insist doing the cleanup in that stage.

This should fix SF bug ##497111: active_limbo_lock error at program
exit.

2.1.2 and 2.2.1 Bugfix candidate!
2001-12-28 22:07:09 +00:00
Tim Peters b64bec3ec0 Whitespace normalization. 2001-09-18 02:26:39 +00:00
Martin v. Löwis 44f8696171 Patch #428326: New class threading.Timer. 2001-09-05 13:44:54 +00:00