开发者

Python, Asyncore and forks

开发者 https://www.devze.com 2022-12-11 06:18 出处:网络
Just for starters, I used Twisted and SocketServer with both ForkMixIn, ThreadMixIn and tried the \"thread-pool\" recepies.

Just for starters, I used Twisted and SocketServer with both ForkMixIn, ThreadMixIn and tried the "thread-pool" recepies.

However, I wanted to make something particular work in Python.

Alittle background. Previously I wrote in C a simple TCP deamon that would bind to a socket and listen on it, then pre-fork X many times and then just pass the serversocket desc to all the forks and everyone would accept the clients very very marrily.

I checked out the "select/poll" based asyncore which I like alot. My only beef was that I could get a little CPU unbound by forking a few times to take advantage of the multi-cpu machine and hope for the best with scheduling.

I cant make it work for the life of me. Only 1 single instance can accept connections, all others simply throw an exception on handling the connect, 'can not iterate thru Empty'.

is this even feasible? I checked alot, but I couldnt find ANY code for forking asyncore dispatchers (cry)

Thank you!

Update 1: (Full traceback as requested)

error: uncaptured python exception, closing channel <__main__.EchoServer listening 0.0.0.0:8001 at 0x2ad4880c93f8> (<type 'exceptions.TypeError'>:'NoneType' /python2.6/asyncore.py|readwrite|99] [/usr/local/python2.6.9/lib/python2.6/asyncore.py|handle_read_event|408] [./6py-server.py|handle_accept|87])

Always happens in accept, regardless if I fork before the asyncore.loop, etc.

Update 2: (full source) pastebined source开发者_如何学编程


You should use code markup for traceback, otherwise it's displayed messed and we don't see exception type.

But I believe it's TypeError: 'NoneType' object is not iterable since self.accept() can return None. The reason is that several processes can get read event for listening socket, but only one can accept it. The rest processes will get EWOULDBLOCK error which is caught, but then it returns None instead of connection-address pair.

Change your handle_accept() to return immediately when accept() returns None.

0

精彩评论

暂无评论...
验证码 换一张
取 消