简体   繁体   中英

How to implement 'workers' correctly

I created a simple service to allow user convert a string to upper case.

def upper_service(s):
    return s.upper()

Now, suppose I want only maximum 1000 workers to do my job (corresponding 1000 concurrent users at a time), but I don't want to create all 1000 workers immediately, just create when needed (an user sends a request to server) and max workers is 1000. If more than 1000 concurrent users, other requests will be added to a 'queue' or something like that.

So how can I do that with python? My approach is good enough or can be improved?

Try something like this:

import queue, threading, time, random, datetime, webbrowser, urllib.request

def main():
    n = 1000
    startTime = datetime.datetime.now()
    q = queue.Queue()
    threads = []

    for i in range(0,n):
        t = threading.Thread(target=async_shit, args=(q, i))
        t.daemon = False
        threads.append(t)

    [x.start() for x in threads]
    [x.join() for x in threads]
    ....

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