def send_proc(task=None): # if server is in a remote network, use 'peer' as (optionally enabling # streaming for efficiency): # yield pycos.Pycos.instance().peer(pycos.Location('server node/ip', port)) server = yield pycos.Task.locate('chat_server', timeout=5) if not server: print('Could not locate server') raise StopIteration server.send(('join', task)) client_id = yield task.receive() # channel is at same location as server task channel = yield pycos.Channel.locate('chat_channel', server.location) recv_task = pycos.Task(recv_proc, client_id) yield channel.subscribe(recv_task) # since readline is synchronous (blocking) call, use async thread async_threads = pycos.AsyncThreadPool(1) if sys.version_info.major > 2: read_input = input else: read_input = raw_input while True: try: line = yield async_threads.async_task(read_input) line = line.strip() if line.lower() in ('quit', 'exit'): break except: break # send message to channel channel.send((line, client_id)) server.send(('quit', client_id)) yield channel.unsubscribe(recv_task)
def method(): if sys.version_info.major > 2: read_input = input else: read_input = raw_input thread_pool = pycos.AsyncThreadPool(4) while True: line = yield thread_pool.async_task(read_input) line = line.strip() print(threading.current_thread(), line)
def client_send(sock, task=None): # since readline is synchronous (blocking) call, use async thread thread_pool = pycos.AsyncThreadPool(1) if sys.version_info.major > 2: read_input = input else: read_input = raw_input while True: try: line = yield thread_pool.async_task(read_input) line = line.strip() if line in ('quit', 'exit'): break except: break yield sock.send_msg(line.encode())
def ws_send(conn,ws, task=None): task.set_daemon() thread_pool = pycos.AsyncThreadPool(1) while True: try: line = yield thread_pool.async_task(ws.recv) except Exception as ex: print 'Error in server tunnel: %s'%(str(ex)) break if not line: break yield conn.send(line) print('End of server tunnel!') os.kill(os.getpid(), signal.SIGTERM)
def client_send(sock, task=None): # since readline is synchronous (blocking) call, use async thread; # alternately, input can be read in 'main' and sent to this task (with # message passing) thread_pool = pycos.AsyncThreadPool(1) if sys.version_info.major > 2: read_input = input else: read_input = raw_input while True: try: line = yield thread_pool.async_task(read_input) line = line.strip() if line in ('quit', 'exit'): break yield sock.send_msg(line.encode()) except: break