Search code examples
python-3.xpython-multithreading

How to Convert a Single-Thread Code to a Multi-Threaded one


I'm having an issue where I declare the currentPlace string as global but I think if I am correct once I convert to str it overrides the global setting correct? I am at an all time loss with this. Any help would be appreciated thank you.

Code:

    from proxy_checker import ProxyChecker
    import json
    import threading
    
    
    
    
    # define an empty list
    places = []
    threads = []
    
    def check():
        global check
        # open file and read the content in a list
        with open('prox_list.txt', 'r') as filehandle:
            for line in filehandle:
            # remove linebreak which is the last character of the string
                global currentPlace
                currentPlace = line[:-1]
            # add item to the list
                places.append(currentPlace)
                checker = ProxyChecker()
                output = checker.check_proxy(str(currentPlace))
                print(str(currentPlace) + " " + str(output))
    
    
        with open('output_prox.txt', 'w') as filehandle:
            json.dump(currentPlace, filehandle)
    
    
    
    for i,link in enumerate(str(currentPlace)):
        t = threading.Thread(target=check, args=(i, link))
        t.start()
        threads.append(t)
    
    for thread in threads:
        thread.join()

Solution

  • You can use the multiprocessing.dummy.Pool class for an easy interface to multi-threading. (It's called "dummy" because it's not really multi-processing, it just lives in the same module.)

    Write your worker function so that it accepts arguments and returns its value (like you would write any other function) and avoid global variables completely. Use the Pool#map method to map a worker function over a list of input values:

    from multiprocessing.dummy import Pool as ThreadPool
    from multiprocessing import cpu_count
    from proxy_checker import ProxyChecker
    
    def check(place):
        checker = ProxyChecker()
        output = checker.check_proxy(place)
        return place, output
    
    places = []
    with open('prox_list.txt', encoding='utf8') as filehandle:
        for line in filehandle:
            places.append(line[:-1])
    
    pool = ThreadPool(cpu_count())
    
    results = pool.map(check, places)
    print(results)