Awesome. Works really fast now. Thanks for the quick response.
The last issue i need to solve is to be able to read from the socket.
However the below code results in a wait for the read before it sends.
I want to be able to send and receive at the same time. Can you help?
Thanks again.
#######################################
import sys
import time
import random
from cogen.core import sockets
from cogen.core import schedulers
from cogen.core.coroutines import coroutine
from cogen.core.events import Sleep
from datetime import datetime
@coroutine
def server():
srv = sockets.Socket()
adr = ('0.0.0.0', len(sys.argv)>1 and int(sys.argv[1]) or 1200)
srv.bind(adr)
srv.listen(64)
while 1:
print "Listening on", adr
conn, addr = yield srv.accept()
print "Connection from %s:%s" % addr
m.add(handler, args=(conn, addr))
@coroutine
def writer():
market_prices = ['EURUSD: 1.3456','USDJPY: 8.4512','GOOGLE:
23.00','USDCHF: 98.4590','NZDEUR: 3.36','EBAY: 5.78']
return '%s - %s\r\n' % (random.choice(market_prices),
datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
@coroutine
def handler(sock, addr):
fh=sock.makefile()
yield fh.write("WELCOME TO ECHO SERVER !\r\n")
yield fh.flush()
while 1:
line = yield fh.readline(1024)
if line.strip() == 'exit':
yield fh.write("GOOD BYE")
yield fh.close()
sock.close()
return
yield fh.write(line)
yield fh.flush()
send_prices = yield writer()
yield fh.write(send_prices)
yield fh.flush()
yield Sleep(0.003)
m = schedulers.Scheduler()
m.add(server)
m.run()
###############################################################
On Sep 1, 1:34 pm, Ionel Maries Cristian <
ionel...@gmail.com> wrote:
> Hey,
>
> There's no need to have the write yield something that you can just return.
>
> Your performance problems are caused by time.sleep call as it blocks the
> whole cogen main loop (no other coroutine can run in that time). Instead use
> something like "yield cogen.core.events.Sleep(0.01)".
>
> -- ionel
>
> >
cogen+un...@googlegroups.com <
cogen%2Bunsu...@googlegroups.com>.