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