in Education by
If I use socket.makefile and then close the file object as well as the underlying socket, then subsequent calls to read will throw an exception, just as I'd want it to. For example, the following code works as I'd expect: import socket from time import sleep from threading import Thread ADDR = ("localhost", 4321) def listener(sock): client,addr = sock.accept() sleep(1) client.close() sock.close() server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_sock.bind(ADDR) server_sock.listen(1) Thread(target=listener, args=[server_sock]).start() sock = socket.create_connection(ADDR) f = sock.makefile("r+b", bufsize=0) f.close() sock.close() f.read(8) # throws an exception, as I'd expect However, if I make a call to read while the file/socket are still open then that call will block, and then if I close the socket, the read method still doesn't return. In fact, it hangs indefinitely until the socket is closed on the other end. The following code demonstrates this distressing behavior: import socket from time import sleep from threading import Thread ADDR = ("localhost", 4321) def reader(f): print("about to read") print("we read %r" % f.read(8)) print("finished reading") def listener(sock): client, addr = sock.accept() sleep(3) client.close() sock.close() server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_sock.bind(ADDR) server_sock.listen(1) Thread(target=listener, args=[server_sock]).start() sock = socket.create_connection(ADDR) f = sock.makefile("r+b", bufsize=0) Thread(target=reader, args=[f]).start() sleep(1) print("closing pseudo-file and socket") f.close() sock.close() sleep(1) print("we still haven't finished reading!") This is a serious problem for me, since I'd like to make a blocking call to f.read in a thread, but then still be able to close the socket and have the thread return from that call (possibly by throwing an exception) and exit. However, all that happens is that the call blocks forever so long as the other side never closes the socket. So is there any way for Thread1 to call read on the file-like object created by socket.makefile and then have Thread2 shut down the socket in a way that causes Thread1 to stop blocking on its read call? EDIT: I tried re-writing my program to entirely use gevent and its socket and Greenlet approach to multithreading, but my program still does exactly the same thing: from gevent import sleep, socket, spawn ADDR = ("localhost", 4321) def reader(f): print("about to read") print("we read %r" % f.read(8)) print("finished reading") def listener(sock): client, addr = sock.accept() sleep(3) client.close() sock.close() server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_sock.bind(ADDR) server_sock.listen(1) spawn(listener, server_sock) sock = socket.create_connection(ADDR) f = sock.makefile("r+b", bufsize=0) spawn(reader, f) sleep(1) print("closing pseudo-file and socket") f.close() sock.close() sleep(1) print("we still haven't finished reading!") sleep(2) I was surprised to learn that even using gevent sockets, calls to read will block even after the underlying socket is closed. If there's no way to prevent this, I'll probably just have to accept Thomas' depressing "this is not possible" answer :( JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
In situations like this, I've had success using eventlet and nowadays, gevent is doing the same types of things: 'monkey patching' the socket library to use non blocking I/O. Here's an example of what you could try: >>> from gevent import monkey; monkey.patch_socket() >>> import socket and see how that'd affect your results

Related questions

0 votes
    I need to react in a main process to random events happening in a child process. I have implemented ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
0 votes
    Hey there Stack Overflow. I'm trying to build a testing script that should mix outputting changing ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    In the next examlpe I have Socket server handled in the thread. In the handle function we create Thread ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    In the next examlpe I have Socket server handled in the thread. In the handle function we create Thread ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    In the next examlpe I have Socket server handled in the thread. In the handle function we create Thread ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have inherited a corporate server & application that consists of several python scripts, html files, and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    This question already has an answer here: Object.assign getters and setters in constructor (1 answer) Closed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    This question already has an answer here: Object.assign getters and setters in constructor (1 answer) Closed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
0 votes
    Why a dry cell becomes dead after a long time even if it is not used ? Select the correct answer from above options...
asked Jan 5, 2022 in Education by JackTerrance
0 votes
    The client expects a timely response from the service and might even block while it waits. This represents the _________ client ... B. One to one C. One to many D. Synchronous...
asked Jan 10, 2023 in Technology by JackTerrance
0 votes
    There are situations in which it is necessary to write back the block to disk, even though the buffer ... Buffer in chapter Storage and File Structures of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    The method adopted by block cipher modes to generate unique ciphertexts even if the same plaintext is encrypted multiple times ... Vector (2)Random Keys (3)Padding (4)Blockchain...
asked Mar 19, 2021 in Technology by JackTerrance
0 votes
    Since the *.o, *.h file and *.c files are stored in different directories, do I need add prefix ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    What is the syntax of close method for document object? (a) Close(object) (b) Close(doc) (c) ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 21, 2021 in Education by JackTerrance
...