|
12 | 12 | import io |
13 | 13 | import os |
14 | 14 | import sys |
15 | | -import select |
16 | 15 | import socket |
17 | 16 | import struct |
18 | 17 | import errno |
@@ -877,48 +876,30 @@ def wait(object_list, timeout=None): |
877 | 876 |
|
878 | 877 | else: |
879 | 878 |
|
880 | | - if hasattr(select, 'poll'): |
881 | | - def _poll(fds, timeout): |
882 | | - if timeout is not None: |
883 | | - timeout = int(timeout * 1000) # timeout is in milliseconds |
884 | | - fd_map = {} |
885 | | - pollster = select.poll() |
886 | | - for fd in fds: |
887 | | - pollster.register(fd, select.POLLIN) |
888 | | - if hasattr(fd, 'fileno'): |
889 | | - fd_map[fd.fileno()] = fd |
890 | | - else: |
891 | | - fd_map[fd] = fd |
892 | | - ls = [] |
893 | | - for fd, event in pollster.poll(timeout): |
894 | | - if event & select.POLLNVAL: |
895 | | - raise ValueError('invalid file descriptor %i' % fd) |
896 | | - ls.append(fd_map[fd]) |
897 | | - return ls |
898 | | - else: |
899 | | - def _poll(fds, timeout): |
900 | | - return select.select(fds, [], [], timeout)[0] |
901 | | - |
| 879 | + import selectors |
902 | 880 |
|
903 | 881 | def wait(object_list, timeout=None): |
904 | 882 | ''' |
905 | 883 | Wait till an object in object_list is ready/readable. |
906 | 884 |
|
907 | 885 | Returns list of those objects in object_list which are ready/readable. |
908 | 886 | ''' |
909 | | - if timeout is not None: |
910 | | - if timeout <= 0: |
911 | | - return _poll(object_list, 0) |
912 | | - else: |
913 | | - deadline = time.time() + timeout |
914 | | - while True: |
915 | | - try: |
916 | | - return _poll(object_list, timeout) |
917 | | - except OSError as e: |
918 | | - if e.errno != errno.EINTR: |
919 | | - raise |
| 887 | + with selectors.DefaultSelector() as selector: |
| 888 | + for obj in object_list: |
| 889 | + selector.register(obj, selectors.EVENT_READ) |
| 890 | + |
920 | 891 | if timeout is not None: |
921 | | - timeout = deadline - time.time() |
| 892 | + deadline = time.time() + timeout |
| 893 | + |
| 894 | + while True: |
| 895 | + ready = selector.select(timeout) |
| 896 | + if ready: |
| 897 | + return [key.fileobj for (key, events) in ready] |
| 898 | + else: |
| 899 | + if timeout is not None: |
| 900 | + timeout = deadline - time.time() |
| 901 | + if timeout < 0: |
| 902 | + return ready |
922 | 903 |
|
923 | 904 | # |
924 | 905 | # Make connection and socket objects sharable if possible |
|
0 commit comments