简体   繁体   中英

Threading in Python 3: Spawning a new thread for a specific process

So I've used a bit of threading in python 2.6, but I'm confused how to do it in python 3, and it's been a while. Basically, I have a function that I pass 2 variables. I want to thread that function so that every time it's called, it creates a new thread instance, and when it finishes, the thread closes. It shouldn't be that difficult, but for some reason, after reading a ton of documentation, I can't seem to figure it out. Here's an example. ```

def accept_connection(clientsocket, addr):
    print("Got a connection from %s" % str(addr))  #some serverside debugging output
    msg = clientsocket.recv(1024)   #recieve a message from the client
    ClientAppend = str(addr) + str(msg, 'ascii')
    Clients.append(ClientAppend)
    print(Clients)


accept_connection(clientsocket, addr)

In case it's unclear, I want the accept_connection() function to be threaded, so that I can accept multiple connections at once and they aren't denied because one is already being processed. I'm not really sure how to do this, and I appreciate any and all help I can get.

So what you're looking for has already been nicely described in this here . You want to look into multithread tcp file transfer on localhost .

They have a nice tutorial from scratch which you can use here .

They have a five part tutorial with really simple examples which are well explained. I feel the tutorial would explain much better than I could.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM