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 |
| 20 | |
| 21 | class error(Exception): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 22 | """Dummy implementation of _thread.error.""" |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 23 | |
| 24 | def __init__(self, *args): |
| 25 | self.args = args |
| 26 | |
| 27 | def start_new_thread(function, args, kwargs={}): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 28 | """Dummy implementation of _thread.start_new_thread(). |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 29 | |
| 30 | Compatibility is maintained by making sure that ``args`` is a |
| 31 | tuple and ``kwargs`` is a dictionary. If an exception is raised |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 32 | 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] | 33 | caught and nothing is done; all other exceptions are printed out |
| 34 | by using traceback.print_exc(). |
| 35 | |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 36 | If the executed function calls interrupt_main the KeyboardInterrupt will be |
| 37 | raised when the function returns. |
| 38 | |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 39 | """ |
| 40 | if type(args) != type(tuple()): |
| 41 | raise TypeError("2nd arg must be a tuple") |
| 42 | if type(kwargs) != type(dict()): |
| 43 | raise TypeError("3rd arg must be a dict") |
Brett Cannon | 91012fe | 2003-06-13 23:56:32 +0000 | [diff] [blame] | 44 | global _main |
| 45 | _main = False |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 46 | try: |
| 47 | function(*args, **kwargs) |
| 48 | except SystemExit: |
| 49 | pass |
| 50 | except: |
| 51 | _traceback.print_exc() |
Brett Cannon | 91012fe | 2003-06-13 23:56:32 +0000 | [diff] [blame] | 52 | _main = True |
| 53 | global _interrupt |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 54 | if _interrupt: |
Brett Cannon | 4e64d78 | 2003-06-13 23:44:35 +0000 | [diff] [blame] | 55 | _interrupt = False |
| 56 | raise KeyboardInterrupt |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 57 | |
| 58 | def exit(): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 59 | """Dummy implementation of _thread.exit().""" |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 60 | raise SystemExit |
| 61 | |
| 62 | def get_ident(): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 63 | """Dummy implementation of _thread.get_ident(). |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 64 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 65 | Since this module should only be used when _threadmodule is not |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 66 | available, it is safe to assume that the current process is the |
| 67 | only thread. Thus a constant can be safely returned. |
| 68 | """ |
| 69 | return -1 |
| 70 | |
| 71 | def allocate_lock(): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 72 | """Dummy implementation of _thread.allocate_lock().""" |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 73 | return LockType() |
| 74 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | def stack_size(size=None): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 76 | """Dummy implementation of _thread.stack_size().""" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 77 | if size is not None: |
| 78 | raise error("setting thread stack size not supported") |
| 79 | return 0 |
| 80 | |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 81 | class LockType(object): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 82 | """Class implementing dummy implementation of _thread.LockType. |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 84 | Compatibility is maintained by maintaining self.locked_status |
| 85 | 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] | 86 | 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] | 87 | then used with an unpickled ``lock()`` from here problems could |
| 88 | occur from this class not having atomic methods. |
| 89 | |
| 90 | """ |
| 91 | |
| 92 | def __init__(self): |
| 93 | self.locked_status = False |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 95 | def acquire(self, waitflag=None): |
| 96 | """Dummy implementation of acquire(). |
| 97 | |
| 98 | For blocking calls, self.locked_status is automatically set to |
| 99 | True and returned appropriately based on value of |
| 100 | ``waitflag``. If it is non-blocking, then the value is |
| 101 | actually checked and not set if it is already acquired. This |
| 102 | is all done so that threading.Condition's assert statements |
| 103 | aren't triggered and throw a little fit. |
| 104 | |
| 105 | """ |
Brett Cannon | 40c8f23 | 2008-07-13 01:19:15 +0000 | [diff] [blame] | 106 | if waitflag is None or waitflag: |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 107 | self.locked_status = True |
Brett Cannon | 40c8f23 | 2008-07-13 01:19:15 +0000 | [diff] [blame] | 108 | return True |
| 109 | else: |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 110 | if not self.locked_status: |
| 111 | self.locked_status = True |
| 112 | return True |
| 113 | else: |
| 114 | return False |
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 |