I have an app that uses Uplink for calling server functions from a virtual machine in the cloud. How are concurrent requests handled? If the server-side code on my VM takes 1000ms to execute, what happens if it is called from two different browser sessions 500ms apart?
The Anvil Uplink library spawns a new thread to execute each incoming server function call, so you will need to make sure your functions are thread-safe!
Hope that helps - happy to provide further clarification as needed.
Hi there.
I’m having a similar issue except my sever function runs on Google Colab and takes about a minute to complete. Multi-threading sounds like a good solution but I’m not really sure what you mean by ‘thread-safe’. Please elaborate or provide a link to more details?
Additionally, I was wondering if there is a way to cue the multiple client requests to the same server function coming from multiple browsers.
Regards,
Taher Anjary
Welcome to the Anvil forums!
When a program has multiple “threads”, essentially two or more things can be going on at the same time. Think of two chefs in the same kitchen. They both can’t use the same burner at the same time!
In Python, the equivalent to that “burner” is a global variable. Such a variable holds only one value at a time. If Task A changes it, then Task B changes it to something else, Task A will then see B’s value, and might not behave correctly as a result.
To make a thread-safe program, you must arrange for such conflicts to be prevented or resolved.
This can be trickier than it looks, as global variables can exist in unexpected places. Are your tasks sharing a database connection? A file on disk? Any shared resource might effectively be a global variable.
Here’s a more complete introduction to Threads in Python: https://realpython.com/intro-to-python-threading/
Your idea of queueing up the incoming tasks is a very good one. All an individual thread has to do is push the job into the queue. Then the queue is the only shared object. That keeps the potential for conflict to a minimum.
And, the Python standard library has Queue classes, for just this purpose! See
- https://pymotw.com/3/queue/ and
-
queue
— A synchronized queue class