64 return min(max(countcpus(), 4), 32) |
64 return min(max(countcpus(), 4), 32) |
65 |
65 |
66 |
66 |
67 def ismainthread(): |
67 def ismainthread(): |
68 return threading.current_thread() == threading.main_thread() |
68 return threading.current_thread() == threading.main_thread() |
|
69 |
|
70 |
|
71 class _blockingreader(object): |
|
72 def __init__(self, wrapped): |
|
73 self._wrapped = wrapped |
|
74 |
|
75 # Do NOT implement readinto() by making it delegate to |
|
76 # _wrapped.readinto(), since that is unbuffered. The unpickler is fine |
|
77 # with just read() and readline(), so we don't need to implement it. |
|
78 |
|
79 if (3, 8, 0) <= sys.version_info[:3] < (3, 8, 2): |
|
80 |
|
81 # This is required for python 3.8, prior to 3.8.2. See issue6444. |
|
82 def readinto(self, b): |
|
83 pos = 0 |
|
84 size = len(b) |
|
85 |
|
86 while pos < size: |
|
87 ret = self._wrapped.readinto(b[pos:]) |
|
88 if not ret: |
|
89 break |
|
90 pos += ret |
|
91 |
|
92 return pos |
|
93 |
|
94 def readline(self): |
|
95 return self._wrapped.readline() |
|
96 |
|
97 # issue multiple reads until size is fulfilled |
|
98 def read(self, size=-1): |
|
99 if size < 0: |
|
100 return self._wrapped.readall() |
|
101 |
|
102 buf = bytearray(size) |
|
103 view = memoryview(buf) |
|
104 pos = 0 |
|
105 |
|
106 while pos < size: |
|
107 ret = self._wrapped.readinto(view[pos:]) |
|
108 if not ret: |
|
109 break |
|
110 pos += ret |
|
111 |
|
112 del view |
|
113 del buf[pos:] |
|
114 return bytes(buf) |
69 |
115 |
70 |
116 |
71 class _blockingreader: |
117 class _blockingreader: |
72 def __init__(self, wrapped): |
118 def __init__(self, wrapped): |
73 self._wrapped = wrapped |
119 self._wrapped = wrapped |