Last active
November 12, 2015 17:33
-
-
Save dhable/74122434b7c8abef42f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Check out the differences between the select.poll functionality in CPython | |
and Jython. | |
""" | |
import socket | |
import errno | |
import threading | |
import traceback | |
import logging | |
import select | |
logging.basicConfig(level=logging.DEBUG) | |
logger = logging.getLogger('select_bug') | |
class SocketPollThread(threading.Thread): | |
def __init__(self, address, name): | |
threading.Thread.__init__(self, name=name) | |
self.address = address | |
self.connect_status_results = [] | |
self.final_result = "" | |
self.poller = select.poll() | |
def run(self): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.setblocking(0) | |
try: | |
connect_errno = sock.connect_ex(self.address) | |
if connect_errno != errno.EINPROGRESS: | |
self.final_result = "[%s] socket did not enter EINPROGRESS as expected" % (self.getName(),) | |
return | |
else: | |
self.poller.register(sock.fileno()) | |
fd_map = {sock.fileno(): sock} | |
while True: | |
events = self.poller.poll() | |
for fd, event in events: | |
logger.debug("sock.fileno = " + str(sock.fileno()) + ", fd = " + str(fd)) | |
if sock.fileno() == fd: | |
self.final_result = "fd and sock.fileno() values match" | |
else: | |
self.final_result = "FAIL: fd and sock.fileno() values differ" | |
return | |
except: | |
self.final_result = traceback.format_exc() | |
finally: | |
try: | |
sock.shutdown(0) | |
sock.shutdown(1) | |
sock.close() | |
except: | |
pass | |
if __name__ == "__main__": | |
t = SocketPollThread(address=('127.0.0.1', 8000), name="SelectTest-1") | |
t.start() | |
t.join() | |
logger.debug(t.final_result) |
Author
dhable
commented
Nov 11, 2015
NOTE - I originally thought this was a bug in the Jython poll impl but instead was a misuse of the API. poll.register accepts a fd that needs to be obtained by calling fileno() on a socket, file object, etc.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment