Re: [gevent] Sharing data between two greenlets and using Event()

412 views
Skip to first unread message
Message has been deleted

Danil "Eleneldil G. Arilou" Lavrentyuk

unread,
Mar 26, 2013, 4:39:58 AM3/26/13
to gev...@googlegroups.com
It's not clear from your explanation, does the request to the 3rd
party server depend on that 'some processing' you do with client's
request or can be performed immediately.
But, in any case, scheme should by something like this:

def send2server(res, req):
sock = socket.create_connection(('127.0.0.1', 8001), timeout=5.0)
sock.sendall(req)
res.set( self.sock.recv(4096) )
sock.close()

def process_client_request(sock, req):
data, req1 = perform_some_preprocessing(req) # do what you need
before ask the 3rd party
res = AsyncResult()
gevent.spawn(send2server, res, req1)
data = some_other_processing(req, data) # if you need it
sock.sendall(data) # send the part of reply not depending on the 3rd party
sock.sendall( res.get() ) # send the reply from the 3rd party.
res.get() will wait for res.set() in send2server()



2013/3/23 Vin <pythonw...@gmail.com>:
> Hey Guys,
>
> Need your help in using gevent effectively. I have a typical scenario like
> this: I am running gevent's StreamServer. Every time a client sends a
> request to the StreamServer for some information (client can be anybody from
> outside), server does some processing and then sends the request to another
> third party server to get extended information. This third party server
> accepts the request and reply back with extended information. What I want is
> that when the stream server is connecting and sending request to 3rd party
> (I am using gevent socket), stream server should keep sending information to
> client as well (for this I have created two greenlets g1 and g2).
> Currently I am using Events() like this:
>
> def send2server(self, e, req):
> self.sock = socket.create_connection(('127.0.0.1', 8001), timeout=5.0)
> self.sock.sendall(req)
> res = self.sock.recv(4096)
> self.sock.close()
> e.set()
>
> def to_client(self, e, req):
> while not e.is_set():
> streamsock(req) # sending to client
>
> #TODO get info from g1 and send that to client
> return
>
> e = Event()
> e.clear()
> req1 = 'extended'
> req2 = 'waiting...'
>
> result = gevent.joinall([
> gevent.spawn(send2server, e, req1), # g1
> gevent.spawn(to_client, e, req2), # g2
> ])
>
>
> What I am not able to achieve is that how to send information received by g1
> to g2. Also, I want to know will that affect other greenlets that are
> spawned by new client connection to StreamServer as the server is getting
> many requests in parallel. I want every connection to remain separate and
> not share information or affect another one.
>
> Can this be achieved using AsyncResult with all the conditions of the case
> fulfilled?
>
> Regards,
> Vin
>
> --
> You received this message because you are subscribed to the Google Groups
> "gevent: coroutine-based Python network library" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to gevent+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Reply all
Reply to author
Forward
0 new messages