Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a method without expecting a response

Tags:

python

I'm trying to create a server-side application to listen for connections and call a method based on what the client says. However when I call the method upload i want to continue executing code under main.

Is there anyway i can achieve this, or am i taking the incorrect approach? Code below;

def main(ipAddr, tcpPort, bufferSize, s):
    try:
        s.bind((ipAddr, tcpPort))
        s.listen(4)
        conn, addr = s.accept()

    print("Connection attempt from: %s" % addr)
    messageRecv = ""
    while True:
        data = conn.recv(bufferSize)
        if not data: break
        messageRecv = data.decode('utf-8')
finally:
    conn.close()
    if messageRecv == "Ready": upload(addr)

main(ipAddr, tcpPort, bufferSize, s)



def upload(addr):
    pass
like image 580
PacketLoss Avatar asked Sep 09 '26 03:09

PacketLoss


1 Answers

Consider using the multiprocessing module, which is essentially Python's "multithreading" package. The API is very similar to the threading module's, but threading may be prohibitive due to the GIL.

In your case, you may have something like this:

p = Process(target=upload, args=(addr,))
p.start()
# some other code
p.join()
like image 81
arshajii Avatar answered Sep 11 '26 17:09

arshajii