Package afx :: Module servers
[hide private]
[frames] | no frames]

Source Code for Module afx.servers

 1  # -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- 
 2  # vim:et:sw=4:ts=4 
 3   
 4  from __future__ import ( generators, with_statement ) 
 5  from contextlib import contextmanager 
 6  import af 
 7   
 8  __all__ = [ 'server', 'pool' ] 
 9   
10 -class server( object ):
11 - def __init__( self, host, port ):
12 self.s = af.socket.socket() 13 self.s.bind( ( host, port ) ) 14 self.s.listen()
15 - def __enter__( self ): return self
16 - def __exit__( self, *args ): self.s.close()
17 @af.task
18 - def accept( self ):
19 res = yield self.s.accept() 20 yield af.result( res )
21 - def close( self ): self.s.close()
22
23 -class pool( object ):
24 - def __init__( self, *tasks ):
25 self.tasks = [] 26 self.spawn( *tasks )
27 - def spawn( self, *tasks ):
28 self.tasks.extend( af.spawn( t ) for t in tasks )
29 - def __enter__( self ): return self
30 - def __exit__( self, *args ): self.stop()
31 - def stop( self ): [ task.stop() for task in self.tasks ]
32 33 @contextmanager
34 -def stopping( x ):
35 try: yield x 36 finally: x.stop()
37