Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 1 | """Drop-in replacement for the thread module. |
| 2 | |
| 3 | Meant to be used as a brain-dead substitute so that threaded code does |
| 4 | not need to be rewritten for when the thread module is not present. |
| 5 | |
| 6 | Suggested usage is:: |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 8 | try: |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 9 | import _thread |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 10 | except ImportError: |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 11 | import _dummy_thread as _thread |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 12 | |
| 13 | """ |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 14 | # Exports only things specified by thread documentation; |
| 15 | # skipping obsolete synonyms allocate(), start_new(), exit_thread(). |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 16 | __all__ = ['error', 'start_new_thread', 'exit', 'get_ident', 'allocate_lock', |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 17 | 'interrupt_main', 'LockType'] |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 18 | |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 19 | # A dummy value |
| 20 | TIMEOUT_MAX = 2**31 |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 21 | |
Antoine Pitrou | db1bad2 | 2010-11-05 19:58:28 +0000 | [diff] [blame] | 22 | # NOTE: this module can be imported early in the extension building process, |
| 23 | # and so top level imports of other modules should be avoided. Instead, all |
| 24 | # imports are done when needed on a function-by-function basis. Since threads |
| 25 | # are disabled, the import lock should not be an issue anyway (??). |
| 26 | |
Antoine Pitrou | 819c40f | 2011-03-01 23:05:42 +0000 | [diff] [blame] | 27 | error = RuntimeError |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 28 | |
| 29 | def start_new_thread(function, args, kwargs={}): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 30 | """Dummy implementation of _thread.start_new_thread(). |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 31 | |
| 32 | Compatibility is maintained by making sure that ``args`` is a |
| 33 | tuple and ``kwargs`` is a dictionary. If an exception is raised |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 34 | and it is SystemExit (which can be done by _thread.exit()) it is |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 35 | caught and nothing is done; all other exceptions are printed out |
| 36 | by using traceback.print_exc(). |
| 37 | |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 38 | If the executed function calls interrupt_main the KeyboardInterrupt will be |
| 39 | raised when the function returns. |
| 40 | |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 41 | """ |
| 42 | if type(args) != type(tuple()): |
| 43 | raise TypeError("2nd arg must be a tuple") |
| 44 | if type(kwargs) != type(dict()): |
| 45 | raise TypeError("3rd arg must be a dict") |
Brett Cannon | 91012fe | 2003-06-13 23:56:32 +0000 | [diff] [blame] | 46 | global _main |
| 47 | _main = False |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 48 | try: |
| 49 | function(*args, **kwargs) |
| 50 | except SystemExit: |
| 51 | pass |
| 52 | except: |
Antoine Pitrou | db1bad2 | 2010-11-05 19:58:28 +0000 | [diff] [blame] | 53 | import traceback |
| 54 | traceback.print_exc() |
Brett Cannon | 91012fe | 2003-06-13 23:56:32 +0000 | [diff] [blame] | 55 | _main = True |
| 56 | global _interrupt |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 57 | if _interrupt: |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 58 | _interrupt = False |
| 59 | raise KeyboardInterrupt |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 60 | |
| 61 | def exit(): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 62 | """Dummy implementation of _thread.exit().""" |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 63 | raise SystemExit |
| 64 | |
| 65 | def get_ident(): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 66 | """Dummy implementation of _thread.get_ident(). |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 67 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 68 | Since this module should only be used when _threadmodule is not |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 69 | available, it is safe to assume that the current process is the |
| 70 | only thread. Thus a constant can be safely returned. |
| 71 | """ |
| 72 | return -1 |
| 73 | |
| 74 | def allocate_lock(): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 75 | """Dummy implementation of _thread.allocate_lock().""" |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 76 | return LockType() |
| 77 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | def stack_size(size=None): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 79 | """Dummy implementation of _thread.stack_size().""" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 80 | if size is not None: |
| 81 | raise error("setting thread stack size not supported") |
| 82 | return 0 |
| 83 | |
Antoine Pitrou | 7b47699 | 2013-09-07 23:38:37 +0200 | [diff] [blame] | 84 | def _set_sentinel(): |
| 85 | """Dummy implementation of _thread._set_sentinel().""" |
| 86 | return LockType() |
| 87 | |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 88 | class LockType(object): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 89 | """Class implementing dummy implementation of _thread.LockType. |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 91 | Compatibility is maintained by maintaining self.locked_status |
| 92 | which is a boolean that stores the state of the lock. Pickling of |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 93 | the lock, though, should not be done since if the _thread module is |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 94 | then used with an unpickled ``lock()`` from here problems could |
| 95 | occur from this class not having atomic methods. |
| 96 | |
| 97 | """ |
| 98 | |
| 99 | def __init__(self): |
| 100 | self.locked_status = False |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 101 | |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 102 | def acquire(self, waitflag=None, timeout=-1): |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 103 | """Dummy implementation of acquire(). |
| 104 | |
| 105 | For blocking calls, self.locked_status is automatically set to |
| 106 | True and returned appropriately based on value of |
| 107 | ``waitflag``. If it is non-blocking, then the value is |
| 108 | actually checked and not set if it is already acquired. This |
| 109 | is all done so that threading.Condition's assert statements |
| 110 | aren't triggered and throw a little fit. |
| 111 | |
| 112 | """ |
Brett Cannon | 40c8f23 | 2008-07-13 01:19:15 +0000 | [diff] [blame] | 113 | if waitflag is None or waitflag: |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 114 | self.locked_status = True |
Brett Cannon | 40c8f23 | 2008-07-13 01:19:15 +0000 | [diff] [blame] | 115 | return True |
| 116 | else: |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 117 | if not self.locked_status: |
| 118 | self.locked_status = True |
| 119 | return True |
| 120 | else: |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 121 | if timeout > 0: |
Antoine Pitrou | db1bad2 | 2010-11-05 19:58:28 +0000 | [diff] [blame] | 122 | import time |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 123 | time.sleep(timeout) |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 124 | return False |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 125 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 126 | __enter__ = acquire |
| 127 | |
| 128 | def __exit__(self, typ, val, tb): |
| 129 | self.release() |
| 130 | |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 131 | def release(self): |
| 132 | """Release the dummy lock.""" |
| 133 | # XXX Perhaps shouldn't actually bother to test? Could lead |
| 134 | # to problems for complex, threaded code. |
| 135 | if not self.locked_status: |
| 136 | raise error |
| 137 | self.locked_status = False |
| 138 | return True |
| 139 | |
| 140 | def locked(self): |
| 141 | return self.locked_status |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 142 | |
Raymond Hettinger | 62f4dad | 2014-05-25 18:22:35 -0700 | [diff] [blame] | 143 | def __repr__(self): |
| 144 | return "<%s %s.%s object at %s>" % ( |
| 145 | "locked" if self.locked_status else "unlocked", |
| 146 | self.__class__.__module__, |
| 147 | self.__class__.__qualname__, |
| 148 | hex(id(self)) |
| 149 | ) |
| 150 | |
Brett Cannon | 91012fe | 2003-06-13 23:56:32 +0000 | [diff] [blame] | 151 | # Used to signal that interrupt_main was called in a "thread" |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 152 | _interrupt = False |
Brett Cannon | 91012fe | 2003-06-13 23:56:32 +0000 | [diff] [blame] | 153 | # True when not executing in a "thread" |
| 154 | _main = True |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 155 | |
| 156 | def interrupt_main(): |
| 157 | """Set _interrupt flag to True to have start_new_thread raise |
| 158 | KeyboardInterrupt upon exiting.""" |
Brett Cannon | 91012fe | 2003-06-13 23:56:32 +0000 | [diff] [blame] | 159 | if _main: |
| 160 | raise KeyboardInterrupt |
| 161 | else: |
| 162 | global _interrupt |
| 163 | _interrupt = True |