python grpc worker or thread setting

1,517 views
Skip to first unread message

he...@snaps.com

unread,
Jan 4, 2018, 9:25:26 PM1/4/18
to grpc.io
Hi! I'm Heidi.
I'm trying to build an api with python.
I try to build with grpc.
Post the image file to grpc server.
The gprc server detects the object by deep-running the requested image file.

First question: How do I transfer an image file to the server?

Show me an example.
For example, when sending a string:
response = stub.SayHello (helloworld_pb2.HelloRequest (name = 'you'))


Second question. I want to request grpc server concurrently, not sequential. What should I do?

I run one server .py. Then I run client.py at the same time. But it seems to be running sequentially.
For example, one client.py

Second question. I want to request grpc server concurrently, not sequential. What should I do?

I run one server .py. Then I run client.py at the same time. But it seems to be running sequentially.
For example, it takes 1 second to run one client.py.
But if you run 3 client.py at the same time, it takes 3 seconds.


I want to know how to set up grpc worker or thread. Please let me know python code example.

Ken Payson

unread,
Jan 4, 2018, 9:39:45 PM1/4/18
to he...@snaps.com, grpc.io
On Thu, Jan 4, 2018 at 6:25 PM, <he...@snaps.com> wrote:
Hi! I'm Heidi.
I'm trying to build an api with python.
I try to build with grpc.
Post the image file to grpc server.
The gprc server detects the object by deep-running the requested image file.

First question: How do I transfer an image file to the server?
The easiest way to do this to convert the image to a string, and create a simple protobuf with a string field.

This might not be practical depending on the size of your images, but its a good place to start.  You can
explore using a streaming API if your images are too large.


Show me an example.
For example, when sending a string:
response = stub.SayHello (helloworld_pb2.HelloRequest (name = 'you'))


Second question. I want to request grpc server concurrently, not sequential. What should I do?

I run one server .py. Then I run client.py at the same time. But it seems to be running sequentially.
For example, one client.py

Second question. I want to request grpc server concurrently, not sequential. What should I do?

I run one server .py. Then I run client.py at the same time. But it seems to be running sequentially.
For example, it takes 1 second to run one client.py.
But if you run 3 client.py at the same time, it takes 3 seconds.


I want to know how to set up grpc worker or thread. Please let me know python code example.

Python gRPC servers can handle concurrent requests.  Look at examples/python/helloworld/greeter_server.py for an example:

server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

max_workers is the number of concurrent requests the Python server can handle.

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/ddcb37cd-371c-41c7-af3c-1ede967f8046%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

he...@snaps.com

unread,
Jan 4, 2018, 10:19:06 PM1/4/18
to grpc.io
my server.py code:



------------------------------------------------
from concurrent import futures
import time
import os
import grpc

import helloworld_pb2
import helloworld_pb2_grpc


## my function
from my_function import my_class


_ONE_DAY_IN_SECONDS = 60 * 60 * 24


class Greeter(helloworld_pb2_grpc.GreeterServicer):

def SayHello(self, request, context):
    ## run function
a = my_class().detect_face()
    return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)


def serve():
server = grpc.server(futures.ThreadPoolExecutor(
max_workers=20))
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
server_port = os.environ.get('PORT', 50051)
server.add_insecure_port('[::]:' + str(server_port))
server.start()

print("==== SERVER RUNNING =====")
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)

if __name__ == '__main__':

serve()

--------------------------------------------

Like my code, I set the number of worker to 20.
And I added my function inside SayHello code.
When I run server.py and run client.py, there is no error.
There are 20 workers, but it slows down when I request client.py at the same time.

Only three requests at the same time are three times slower.
Thank you.






2018년 1월 5일 금요일 오전 11시 39분 45초 UTC+9, Ken Payson 님의 말:


To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.

Ken Payson

unread,
Jan 9, 2018, 7:08:09 PM1/9/18
to he...@snaps.com, grpc.io
If you add a 3 second sleep to SayHello and then run

python client.py & python client.py & python client.py & wait

Does this command take 9 seconds? I would expect it to take 3.

To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.

To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.

he...@snaps.com

unread,
Jan 9, 2018, 9:24:06 PM1/9/18
to grpc.io
Thanks for the reply.
As you said, I confirmed that it runs concurrently through the time.sleep () function.
It seems to run concurrently internally.
However, adding the function below is not simultaneous.
I printed a pid, which has the same pid.


----  server.py  -----------------------------------------------------------
def SayHello(self, request, context):
    ## run function
a = my_class().detect_face() ##<<
I printed a pid, which has the same pid.
    return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)


----------------------------------------------------------------------------

Where should I add my own functions? Please help me.
helloworld_pb2.py ?, helloworld_pb2_grpc.py?

Thanks for giving me a link with a simple example.

2018년 1월 10일 수요일 오전 9시 8분 9초 UTC+9, Ken Payson 님의 말:

Ken Payson

unread,
Jan 10, 2018, 1:35:50 AM1/10/18
to he...@snaps.com, grpc.io
On Tue, Jan 9, 2018 at 6:24 PM, <he...@snaps.com> wrote:
Thanks for the reply.
As you said, I confirmed that it runs concurrently through the time.sleep () function.
It seems to run concurrently internally.
However, adding the function below is not simultaneous.
I printed a pid, which has the same pid.


----  server.py  -----------------------------------------------------------
def SayHello(self, request, context):
    ## run function
a = my_class().detect_face() ##<<
I printed a pid, which has the same pid.
    return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)


----------------------------------------------------------------------------

Where should I add my own functions? Please help me.
helloworld_pb2.py ?, helloworld_pb2_grpc.py?

You should add your function in the SayHello() method, as you have done.

It sounds like the concurrency issue is related to the function you are calling, and not gRPC.  
 
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.

To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
Reply all
Reply to author
Forward
0 new messages