new to python network programming is async_chat.push thread-safe? python3.0

davy zhang davyzhang at
Fri Oct 24 05:03:11 CEST 2008

I wrote this server to handle incoming messages in a process using
multiprocessing named "handler", and sending message in a Thread named
"sender",  'cause I think the async_chat object can not pass between

My project is a network gate server with many complex logic handler
behind, so I use multiprocessing to handle them separately and send
back the clients later when done.
To use the server multicore cpu I tried to separate the send and
receive function in different process but it seems can not be done :)

I just get questions about this design:
1. is async_chat.push thread-safe? 'Cause I found random errors
reporting push fifo queue out of index 0 sometimes
2. is the whole design odd in any way?

here is my code

import asyncore, asynchat
import os, socket, string
from multiprocessing import Process,Manager
import pickle
import _thread

PORT = 80

policyRequest = b"<policy-file-request/>"
policyReturn = b"""<cross-domain-policy>
             <allow-access-from  domain="*"  to-ports="*"  />
             </cross-domain-policy> \x00"""

def handler(taskList,msgList):
    while 1:
        print('getting task')
        item = pickle.loads(taskList.get())
        print('item before handle ', item)
        item['msg'] += b' hanlded done'

def findClient(id):
    for item in clients:
        if item.idx == id:
            return item

def sender():
    global msgList
    while 1:
        item = pickle.loads(msgList.get())
        #print time()
        c = findClient(item['cid'])
        #print time()
        print('msg sent ',item['msg'])
        #print time()

class HTTPChannel(asynchat.async_chat):

    def __init__(self, server, sock, addr):
        global cid;
        asynchat.async_chat.__init__(self, sock)
        self.set_terminator(b"\x00") = b""
        cid += 1
        self.idx = cid
        if not self in clients:

    def collect_incoming_data(self, data): = + data

    def found_terminator(self):
        global taskList
        if == policyRequest:
            d = {'cid':self.idx,'msg'}
            taskList.put(pickle.dumps(d)) = b""

    def handle_close(self):
        if self in clients:

class HTTPServer(asyncore.dispatcher):

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

    def handle_accept(self):
        conn, addr = self.accept()
        HTTPChannel(self, conn, addr)

# try it out
if __name__ == "__main__":
    s = HTTPServer(PORT)
    print ("serving at port", PORT, "...")

    #clients sock obj list stored for further use

    #client id auto increasement
    cid = 0

    manager = Manager()
    taskList = manager.Queue()
    msgList = manager.Queue()

    h = Process(target=handler,args=(taskList,msgList))

    print('entering loop')


More information about the Python-list mailing list