Anvil Server Code Multiprocessing

Hi,

Wondering about server code, do I need use mutiprocessing in order to handle/process more than one connection concurrently?

For example in Python I would write : -
tup_svr_addr_port = (‘127.0.0.1’, 8080)
obj_listening_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    obj_listening_socket.bind(tup_svr_addr_port)
except:
    print('Unable to bind port')

try:
    obj_listening_socket.listen()
except:
    print('Unable to listen on port')

while 1:
    try:
        obj_client_conn_socket, client_addr = obj_listening_socket.accept()
    except:
        print('unable to accept conn.')

‘’’ each time a client connects to the listening port the connection is accepted and a separate a process
is launched to handle the connection these can run in parallel’‘’
obj_process = multiprocessing.Process(target=conn_handler, args=(obj_client_conn_socket,))
obj_process.start()

obj_listening_socket.close() 

So each time my servers accept a connection from a client, a new process is launched that handles that connection. My main Python process is never busy handling a connection, it just spawns a process. This way my server can scale up to use as many cores and handle a huge number of concurrent processes. I’m wondering if Anvil is clever enough to handle events from one client browser as well be able to concurrently process another browsers request or do I need to use multi-processing in the actual server code?

Thanks
Haider

Anvil handles all that for you behind the scenes. Every server call runs in its own Python instance that is spun up for that server call.

3 Likes