Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

_dummy_thread.py 5.0 KB

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

Press p or to see the previous file or, n or to see the next file

Comments

Loading...