Skip to content

Commit ccc135c

Browse files
Do not use select.select() on Windows.
1 parent e4ee49f commit ccc135c

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

Lib/test/test_concurrent_futures/test_interpreter_pool.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import contextlib
33
import io
44
import os
5-
import select
5+
import sys
66
import time
77
import unittest
88
from concurrent.futures.interpreter import (
@@ -17,6 +17,46 @@
1717
from .util import BaseTestCase, InterpreterPoolMixin, setup_module
1818

1919

20+
WINDOWS = sys.platform.startswith('win')
21+
22+
23+
@contextlib.contextmanager
24+
def nonblocking(fd):
25+
blocking = os.get_blocking(fd)
26+
if blocking:
27+
os.set_blocking(fd, False)
28+
try:
29+
yield
30+
finally:
31+
if blocking:
32+
os.set_blocking(fd, blocking)
33+
34+
35+
def read_file_with_timeout(fd, nbytes, timeout):
36+
with nonblocking(fd):
37+
end = time.time() + timeout
38+
try:
39+
return os.read(fd, nbytes)
40+
except BlockingIOError:
41+
pass
42+
while time.time() < end:
43+
try:
44+
return os.read(fd, nbytes)
45+
except BlockingIOError:
46+
continue
47+
else:
48+
raise TimeoutError('nothing to read')
49+
50+
51+
if not WINDOWS:
52+
import select
53+
def read_file_with_timeout(fd, nbytes, timeout):
54+
r, _, _ = select.select([fd], [], [], timeout)
55+
if fd not in r:
56+
raise TimeoutError('nothing to read')
57+
return os.read(fd, nbytes)
58+
59+
2060
def noop():
2161
pass
2262

@@ -27,14 +67,12 @@ def write_msg(fd, msg):
2767

2868

2969
def read_msg(fd, timeout=10.0):
30-
r, _, _ = select.select([fd], [], [], timeout)
31-
if fd not in r:
32-
raise TimeoutError('nothing to read')
3370
msg = b''
34-
while ch := os.read(fd, 1):
35-
if ch == b'\0':
36-
return msg
71+
ch = read_file_with_timeout(fd, 1, timeout)
72+
while ch != b'\0':
3773
msg += ch
74+
ch = os.read(fd, 1)
75+
return msg
3876

3977

4078
def get_current_name():

0 commit comments

Comments
 (0)