#!/usr/bin/python3 import select import socket import threading # Mutex to print messages from multiple threads lock = threading.Lock() def epoll_wait_thread(epfd): lock.acquire() print(threading.currentThread().getName(), " epfd", epfd.fileno(), ": start polling", sep='') lock.release() events = epfd.poll(3) lock.acquire() print(threading.currentThread().getName(), " epfd", epfd.fileno(), ": got events: ", len(events), sep='') lock.release() # Create a connection s1, s2 = socket.socketpair(socket.AF_UNIX) # Create epoll descriptor and register a socket epfd = select.epoll() epfd.register(s1.fileno(), select.EPOLLIN) print(threading.currentThread().getName(), ": created epfd", epfd.fileno(), sep='') # Start 2 threads with epoll_wait() routine threads = [] for i in range(2): thread = threading.Thread(target=epoll_wait_thread, args=(epfd,)) thread.start() threads.append(thread) # Send some data to unblock epoll_wait() threads lock.acquire() print(threading.currentThread().getName(), ": Send some data", sep='') lock.release() s2.sendall(b'qwerty') # Cleanup for thread in threads: thread.join() epfd.close() s1.close() s2.close()