글

라벨이 select인 게시물 표시

python socket select timeout example

본 포스트는 python socket에서 리눅스의 select와 동일한 기능을 하는 select 모듈 사용 예제를 싣고 있다. 이전 포스트에서 non-blocking socket 프로그래밍에 대해 알아봤었다. 아래 링크의 포스트에서 만들어진 예제에 'select' 모듈을 추가해 예제 코드를 만들었다. python socket API 와 non-blocking socket server client example code 1. python socket select timeout example 이전 포스트의 클라이언트 코드에서 select를 사용하지 않은 코드는 아래와 같다. import socket import time def recv(sock,size,timeout_ms):     while timeout_ms > 0:         try:             data = sock.recv(size)             if data is not None:                 return data.decode()         except BlockingIOError:             timeout_ms -= 1             time.sleep(0.001)              return None 위 코드를 수정해 select를 사용하면 아래와 같이 구현...

linux 환경에서 select를 사용한 Sleep함수

#include <sys/select.h> #include <sys/time.h> #include <sys/types.h> /*Sleep milliseconds */ void Sleep_ms( long ms ) {        struct timeval stTimeVal;        stTimeVal.tv_sec = ms / 1000;        ms -= (stTimeVal.tv_sec * 1000);        stTimeVal.tv_usec = ms * 1000;        select(0, 0, 0, 0, &stTimeVal); } /*Sleep microseconds */ void Sleep_usec( long usec ) {        struct timeval stTimeVal;        stTimeVal.tv_sec = usec / 1000000;        usec -= (stTimeVal.tv_sec * 1000000);        stTimeVal.tv_usec = usec ;        select(0, 0, 0, 0, &stTimeVal); }