Take the 2-minute tour ×

I have been coding a blocking client-server in python. how can I change it to nonblocking without thread?

share|improve this question
What exactly do you want to be nonblocking? –  icedwater Nov 1 '13 at 7:43
See asynchronous programming in python –  Janne Karila Nov 1 '13 at 7:52
I have chat server actually, I want to one clinet does not oblige waiting for another one. I mean it does not send orderly. –  user2944170 Nov 1 '13 at 8:25
add comment

1 Answer

up vote 0 down vote accepted

Check out the asyncore and asynchat Python modules. Here's a basic echo server (taken from the asyncore documentation):

import asyncore
import socket

class EchoHandler(asyncore.dispatcher_with_send):

    def handle_read(self):
        data = self.recv(8192)
        if data:

class EchoServer(asyncore.dispatcher):

    def __init__(self, host, port):
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.bind((host, port))

    def handle_accept(self):
        pair = self.accept()
        if pair is not None:
            sock, addr = pair
            print 'Incoming connection from %s' % repr(addr)
            handler = EchoHandler(sock)

server = EchoServer('localhost', 8080)
share|improve this answer
thank you. do you know how can I make something same as this with setblocking? –  user2944170 Nov 1 '13 at 9:50
You can directly use the low-level socket module. It has the setblocking() call to change the blocking behavior. In nonblocking mode, all read and write calls raise exceptions if the read buffer is empty or the write buffer is full. See docs.python.org/2/library/socket.html#socket.socket.setblocking –  sjaensch Nov 1 '13 at 14:47
thank u. it was useful –  user2944170 Nov 1 '13 at 21:21
add comment

Your Answer


