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 |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [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 | |
| 19 | import traceback as _traceback |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 20 | import time |
| 21 | |
| 22 | # A dummy value |
| 23 | TIMEOUT_MAX = 2**31 |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 24 | |
| 25 | class error(Exception): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 26 | """Dummy implementation of _thread.error.""" |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 27 | |
| 28 | def __init__(self, *args): |
| 29 | self.args = args |
| 30 | |
| 31 | def start_new_thread(function, args, kwargs={}): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 32 | """Dummy implementation of _thread.start_new_thread(). |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 33 | |
| 34 | Compatibility is maintained by making sure that ``args`` is a |
| 35 | tuple and ``kwargs`` is a dictionary. If an exception is raised |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 36 | 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] | 37 | caught and nothing is done; all other exceptions are printed out |
| 38 | by using traceback.print_exc(). |
| 39 | |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 40 | If the executed function calls interrupt_main the KeyboardInterrupt will be |
| 41 | raised when the function returns. |
| 42 | |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 43 | """ |
| 44 | if type(args) != type(tuple()): |
| 45 | raise TypeError("2nd arg must be a tuple") |
| 46 | if type(kwargs) != type(dict()): |
| 47 | raise TypeError("3rd arg must be a dict") |
Brett Cannon | 91012fe | 2003-06-13 23:56:32 +0000 | [diff] [blame] | 48 | global _main |
| 49 | _main = False |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 50 | try: |
| 51 | function(*args, **kwargs) |
| 52 | except SystemExit: |
| 53 | pass |
| 54 | except: |
| 55 | _traceback.print_exc() |
Brett Cannon | 91012fe | 2003-06-13 23:56:32 +0000 | [diff] [blame] | 56 | _main = True |
| 57 | global _interrupt |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 58 | if _interrupt: |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 59 | _interrupt = False |
| 60 | raise KeyboardInterrupt |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 61 | |
| 62 | def exit(): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 63 | """Dummy implementation of _thread.exit().""" |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 64 | raise SystemExit |
| 65 | |
| 66 | def get_ident(): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 67 | """Dummy implementation of _thread.get_ident(). |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 68 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 69 | Since this module should only be used when _threadmodule is not |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 70 | available, it is safe to assume that the current process is the |
| 71 | only thread. Thus a constant can be safely returned. |
| 72 | """ |
| 73 | return -1 |
| 74 | |
| 75 | def allocate_lock(): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 76 | """Dummy implementation of _thread.allocate_lock().""" |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 77 | return LockType() |
| 78 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 79 | def stack_size(size=None): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 80 | """Dummy implementation of _thread.stack_size().""" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 81 | if size is not None: |
| 82 | raise error("setting thread stack size not supported") |
| 83 | return 0 |
| 84 | |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 85 | class LockType(object): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 86 | """Class implementing dummy implementation of _thread.LockType. |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 88 | Compatibility is maintained by maintaining self.locked_status |
| 89 | 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] | 90 | 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] | 91 | then used with an unpickled ``lock()`` from here problems could |
| 92 | occur from this class not having atomic methods. |
| 93 | |
| 94 | """ |
| 95 | |
| 96 | def __init__(self): |
| 97 | self.locked_status = False |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 98 | |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 99 | def acquire(self, waitflag=None, timeout=-1): |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 100 | """Dummy implementation of acquire(). |
| 101 | |
| 102 | For blocking calls, self.locked_status is automatically set to |
| 103 | True and returned appropriately based on value of |
| 104 | ``waitflag``. If it is non-blocking, then the value is |
| 105 | actually checked and not set if it is already acquired. This |
| 106 | is all done so that threading.Condition's assert statements |
| 107 | aren't triggered and throw a little fit. |
| 108 | |
| 109 | """ |
Brett Cannon | 40c8f23 | 2008-07-13 01:19:15 +0000 | [diff] [blame] | 110 | if waitflag is None or waitflag: |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 111 | self.locked_status = True |
Brett Cannon | 40c8f23 | 2008-07-13 01:19:15 +0000 | [diff] [blame] | 112 | return True |
| 113 | else: |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 114 | if not self.locked_status: |
| 115 | self.locked_status = True |
| 116 | return True |
| 117 | else: |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 118 | if timeout > 0: |
| 119 | time.sleep(timeout) |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 120 | return False |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 121 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 122 | __enter__ = acquire |
| 123 | |
| 124 | def __exit__(self, typ, val, tb): |
| 125 | self.release() |
| 126 | |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 127 | def release(self): |
| 128 | """Release the dummy lock.""" |
| 129 | # XXX Perhaps shouldn't actually bother to test? Could lead |
| 130 | # to problems for complex, threaded code. |
| 131 | if not self.locked_status: |
| 132 | raise error |
| 133 | self.locked_status = False |
| 134 | return True |
| 135 | |
| 136 | def locked(self): |
| 137 | return self.locked_status |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 138 | |
Brett Cannon | 91012fe | 2003-06-13 23:56:32 +0000 | [diff] [blame] | 139 | # Used to signal that interrupt_main was called in a "thread" |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 140 | _interrupt = False |
Brett Cannon | 91012fe | 2003-06-13 23:56:32 +0000 | [diff] [blame] | 141 | # True when not executing in a "thread" |
| 142 | _main = True |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 143 | |
| 144 | def interrupt_main(): |
| 145 | """Set _interrupt flag to True to have start_new_thread raise |
| 146 | KeyboardInterrupt upon exiting.""" |
Brett Cannon | 91012fe | 2003-06-13 23:56:32 +0000 | [diff] [blame] | 147 | if _main: |
| 148 | raise KeyboardInterrupt |
| 149 | else: |
| 150 | global _interrupt |
| 151 | _interrupt = True |