Python教程

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()
这篇关于python 多线程的使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!