What is non-blocking or asynchronous I/O in Node.js?

Synchronous vs Asynchronous Synchronous execution usually refers to code executing in sequence. Asynchronous execution refers to execution that doesn’t run in the sequence it appears in the code. In the following example, the synchronous operation causes the alerts to fire in sequence. In the async operation, while alert(2) appears to execute second, it doesn’t. Synchronous: … Read more

Python socket.accept nonblocking?

You probably want something like select.select() (see documentation). You supply select() with three lists of sockets: sockets you want to monitor for readability, writability, and error states. The server socket will be readable when a new client is waiting. The select() function will block until one of the socket states has changed. You can specify … Read more

Is non-blocking I/O really faster than multi-threaded blocking I/O? How?

The biggest advantage of nonblocking or asynchronous I/O is that your thread can continue its work in parallel. Of course you can achieve this also using an additional thread. As you stated for best overall (system) performance I guess it would be better to use asynchronous I/O and not multiple threads (so reducing thread switching). … Read more

Read timeout using either urllib2 or any other http library

It’s not possible for any library to do this without using some kind of asynchronous timer through threads or otherwise. The reason is that the timeout parameter used in httplib, urllib2 and other libraries sets the timeout on the underlying socket. And what this actually does is explained in the documentation. SO_RCVTIMEO Sets the timeout … Read more

PySerial non-blocking read loop

Using a separate thread is totally unnecessary. Just do this for your infinite while loop instead (Tested in Python 3.2.3). I use this technique in my eRCaGuy_PyTerm serial terminal program here (search the code for inWaiting() or in_waiting). import serial import time # Optional (required if using time.sleep() below) ser = serial.Serial(port=”COM4″, baudrate=9600) while (True): … Read more

What does Python’s socket.recv() return for non-blocking sockets if no data is received until a timeout occurs?

In the case of a non blocking socket that has no data available, recv will throw the socket.error exception and the value of the exception will have the errno of either EAGAIN or EWOULDBLOCK. Example: import sys import socket import fcntl, os import errno from time import sleep s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((‘127.0.0.1’,9999)) fcntl.fcntl(s, fcntl.F_SETFL, … Read more

Non-blocking console input C++

Example using C++11: #include <iostream> #include <future> #include <thread> #include <chrono> static std::string getAnswer() { std::string answer; std::cin >> answer; return answer; } int main() { std::chrono::seconds timeout(5); std::cout << “Do you even lift?” << std::endl << std::flush; std::string answer = “maybe”; //default to maybe std::future<std::string> future = std::async(getAnswer); if (future.wait_for(timeout) == std::future_status::ready) answer = … Read more