How can I use threading in Python?

前端 未结 19 2725
迷失自我
迷失自我 2020-11-21 04:54

I am trying to understand threading in Python. I\'ve looked at the documentation and examples, but quite frankly, many examples are overly sophisticated and I\'m having trou

19条回答
  •  野性不改
    2020-11-21 05:14

    Using the blazing new concurrent.futures module

    def sqr(val):
        import time
        time.sleep(0.1)
        return val * val
    
    def process_result(result):
        print(result)
    
    def process_these_asap(tasks):
        import concurrent.futures
    
        with concurrent.futures.ProcessPoolExecutor() as executor:
            futures = []
            for task in tasks:
                futures.append(executor.submit(sqr, task))
    
            for future in concurrent.futures.as_completed(futures):
                process_result(future.result())
            # Or instead of all this just do:
            # results = executor.map(sqr, tasks)
            # list(map(process_result, results))
    
    def main():
        tasks = list(range(10))
        print('Processing {} tasks'.format(len(tasks)))
        process_these_asap(tasks)
        print('Done')
        return 0
    
    if __name__ == '__main__':
        import sys
        sys.exit(main())
    

    The executor approach might seem familiar to all those who have gotten their hands dirty with Java before.

    Also on a side note: To keep the universe sane, don't forget to close your pools/executors if you don't use with context (which is so awesome that it does it for you)

提交回复
热议问题