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