Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are multiple threads/multiple processes handled in a python Google Cloud Function?

I've been unable to find any documentation on this. How is it handled to run multiple threads on a Google Cloud function in Python? Does it simply not work (e.g. threading module disabled), or does it behave just as threading normally would in a one-CPU environment?

And similarly for multiprocessing - does it just not work (e.g. doesn't let you spawn other processes) or it works but again it's as if there were just one CPU?

like image 306
Claudiu Avatar asked Feb 26 '26 07:02

Claudiu


1 Answers

There is no restriction for multi-threading, you can do it as you want. So, With Cloud Functions, you need to know that the CPU power depends on the quantity of Memory that you set. You have the table here in the pricing page

As you can see, with 4Gb and 8Gb you have a CPU power of 4.8Ghz. Of course, no cloud CPU are at 4.8Ghz, but that means your function has 2vCPU at 2.4Gghz.

If you have several thread the CPU time is shared between all the thread. Of course, if you have a thread that perform a long HTTP call, the other threads can leverage the CPU computing capacity to continue to live. It's not a problem to have several thread on the same CPU. Except if all the thread need the CPU in the same time (compute intensive app for example). The best use case is parallel API call.

If you use low memory configuration, less CPU cycles are allowed to your functions, and thus the multi thread fill be slower/less efficient

However, keep in mind that you pay the CPU ONLY when your Cloud Functions process a request, and thus, the CPU is allowed to your Cloud Functions only during this period. As for result, if you spawn a thread that you want to live outside request context, no CPU will be allowed to perform the computation.

So, if you use multithreading capability, do it only during a request processing windows.

I wrote an article on Cloud Run which has the same underlying platform as Cloud Functions and performed multi cpu tests.

like image 170
guillaume blaquiere Avatar answered Feb 27 '26 20:02

guillaume blaquiere



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!