Python threading 线程池

from concurrent.futures import ThreadPoolExecutor
from concurrent import futures
import time
import random


"""线程死锁 """
# def wait_on_future():
#     f = executor.submit(pow, 5, 2)
#     # This will never complete because there is only one worker thread and
#     # it is executing this function.
#     print(f.result())
#
#
# executor = ThreadPoolExecutor(max_workers=20)
# executor.submit(wait_on_future)


import concurrent.futures
import urllib.request

URLS = ['http://www.foxnews.com/',
        'http://www.cnn.com/',
        'http://europe.wsj.com/',
        'http://www.bbc.co.uk/',
        'http://some-made-up-domain.com/']


"""线程池官方文档示例"""
# Retrieve a single page and report the URL and contents
def load_url(url, timeout):
    with urllib.request.urlopen(url, timeout=timeout) as conn:
        return conn.read()


# We can use a with statement to ensure threads are cleaned up promptly
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    # Start the load operations and mark each future with its URL
    future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
    for future in concurrent.futures.as_completed(future_to_url):
        url = future_to_url[future]
        try:
            data = future.result()
        except Exception as exc:
            print('%r generated an exception: %s' % (url, exc))
        else:
            print('%r page is %d bytes' % (url, len(data)))



"""demo"""
def add(num, timeout=10):
    s = num * random.randint(1, 10)
    print("start %d %d" % (num ,s))
    time.sleep(s)
    print("end %d %d" % (num ,s))
    return num


with ThreadPoolExecutor(max_workers=10) as excutor:  # 创建线程池
    future_list = [excutor.submit(add, num, num) for num in range(5)]  # 提交任务开始执行任务
    # no = [print(fu) for fu in future_list]  # 查看任务状态
    for fu in futures.as_completed(future_list):  # 当future内的任务完成后,返回结果. 谁先执行完,先返回谁
        print(fu.result(), fu)  # 任务完成结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中可以通过threading和concurrent.futures模块来创建线程池。使用threading的方式可以自己手动添加线程到线程池中,而使用concurrent.futures则可以更方便地实现线程池的创建和管理,具体实现可参考以下代码: 使用threading: ```python import threading class ThreadPool: def __init__(self, num_threads): self._threads = [threading.Thread(target=self._worker) for _ in range(num_threads)] self._task_queue = [] self._lock = threading.Lock() for thread in self._threads: thread.start() def submit(self, func, *args, **kwargs): self._task_queue.append((func, args, kwargs)) def _worker(self): while True: with self._lock: if len(self._task_queue) > 0: task = self._task_queue.pop() else: continue func, args, kwargs = task func(*args, **kwargs) # Example usage def print_message(msg): print(f"Message: {msg}") thread_pool = ThreadPool(num_threads=4) for i in range(10): thread_pool.submit(print_message, f"Hello from thread {i}") # Output # Message: Hello from thread 0 # Message: Hello from thread 1 # Message: Hello from thread 2 # Message: Hello from thread 3 # Message: Hello from thread 4 # Message: Hello from thread 5 # Message: Hello from thread 6 # Message: Hello from thread 7 # Message: Hello from thread 8 # Message: Hello from thread 9 ``` 使用concurrent.futures: ```python from concurrent.futures import ThreadPoolExecutor # Example usage def print_message(msg): print(f"Message: {msg}") with ThreadPoolExecutor(max_workers=4) as executor: for i in range(10): executor.submit(print_message, f"Hello from thread {i}") # Output # Message: Hello from thread 0 # Message: Hello from thread 1 # Message: Hello from thread 2 # Message: Hello from thread 3 # Message: Hello from thread 4 # Message: Hello from thread 5 # Message: Hello from thread 6 # Message: Hello from thread 7 # Message: Hello from thread 8 # Message: Hello from thread 9 ``` 以上代码只是一个简单的示例,线程池在实际应用中需要根据具体的场景进行合理的设计和使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值