编程语言
首页 > 编程语言> > python 多线程的使用

python 多线程的使用

作者:互联网

import threadpool

url_list = ['https://www.hao123.com/1', 'https://www.hao123.com/2', 'https://www.hao123.com/3',
            'https://www.hao123.com/4', 'https://www.hao123.com/5', 'https://www.hao123.com/6',
            'https://www.hao123.com/7', 'https://www.hao123.com/8']


def get_data(url):
    print(url)


pool = threadpool.ThreadPool(5)

thread_list = threadpool.makeRequests(get_data, url_list)

for i in thread_list:
    pool.putRequest(i)
pool.wait()
import queue
import threading

url_list = ['https://www.hao123.com/1', 'https://www.hao123.com/2', 'https://www.hao123.com/3',
            'https://www.hao123.com/4', 'https://www.hao123.com/5', 'https://www.hao123.com/6',
            'https://www.hao123.com/7', 'https://www.hao123.com/8']

my_queue = queue.Queue(8)

for i in url_list:
    my_queue.put(i)


def fun1():
    url = my_queue.get()
    print(url)


def fun2():
    url = my_queue.get()
    print(url)


def fun3():
    url = my_queue.get()
    print(url)


t1 = threading.Thread(target=fun1)
t2 = threading.Thread(target=fun2)
t3 = threading.Thread(target=fun3)


t1.start()
t2.start()
t3.start()

t1.join()
t2.join()
t3.join()

标签:www,python,queue,url,https,使用,hao123,多线程,com
来源: https://blog.csdn.net/weixin_44929213/article/details/120319204