Pentium10
Pentium10

Reputation: 207830

How to read CPU cores available in Cloud Run environment?

We see there are environmental variables available to read to get port/service/revision of a Cloud Run service.

In order to configure a multithreaded app, like gulp threads. How do you read how many cores are available?

Snippet:

# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app

Upvotes: 5

Views: 1627

Answers (4)

martin
martin

Reputation: 1328

The nproc command from the coreutils package (man page) can be used to determine the number of processing units available within Cloud Run. Specifically, it returns the number of logical processing units, which typically (if the underlying server utilizes hyper-threading) corresponds to twice the specified CPU limit.

So the docker CMD could be something like:

CMD exec gunicorn --bind :$PORT --workers 1 --threads $(nproc) --timeout 0 main:app

Upvotes: 1

Muhammad Aly
Muhammad Aly

Reputation: 122

The Google Cloud documentation doesnt say anything about how many threads in the Google Cloud run CPU. But after some investigation, I found that Google Cloud Run is built on Google's scalable infrastructure (probably Compute Engine) see this. We can say from the google cloud run pricing documentation that the CPU allocation uses a vCPU which probably supports hyperthreading see this. Hyperthreading is an Intel technology that lets you run multiple threads on the same core.

Again, this is a guess which should not be 100% true.

Upvotes: 1

Ben Butterworth
Ben Butterworth

Reputation: 28472

If you're looking to access multiple CPU cores in Cloud Run, you actually specify the number of cores during configuration. This is a constant value, so you don't need to check for it at runtime, so there is no feature to do so.

https://cloud.google.com/run/docs/configuring/cpu

By default Cloud Run container instances are allocated 1 CPU instance. You can change this number.

Upvotes: 3

Niels Kersic
Niels Kersic

Reputation: 999

I believe this information is not available from within Cloud Run.

Upvotes: 1

Related Questions