收集整理了一份《2024年最新Python全套学习资料》免费送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Python知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来
如果你需要这些资料,可以添加V无偿获取:hxbc188 (备注666)
正文
开启线程
print("Thread-1 is Alive? ", thread1.isAlive())
thread1.start()
thread2.start()
print("Thread-1 is Alive? ", thread1.isAlive())
thread1.join()
thread2.join()
print("Thread-1 is Alive? ", thread1.isAlive())
print(“exit”)
E:\PyPro>python threadClass.py
Thread-1 is Alive? False
Thread Strat:Thread-1
Thread Strat:Thread-2
Thread-1 is Alive? True
Thread-1: Thu Apr 12 10:15:53 2018
Thread-1: Thu Apr 12 10:15:54 2018
Thread-2: Thu Apr 12 10:15:54 2018
Thread-1: Thu Apr 12 10:15:55 2018
Thread-1: Thu Apr 12 10:15:56 2018
Thread-2: Thu Apr 12 10:15:56 2018
Thread-1: Thu Apr 12 10:15:57 2018
Thread Exit:Thread-1
Thread-2: Thu Apr 12 10:15:58 2018
Thread-2: Thu Apr 12 10:16:00 2018
Thread-2: Thu Apr 12 10:16:02 2018
Thread Exit:Thread-2
Thread-1 is Alive? False
exit
不难发现,线程是通过start()函数激活,而不是对象建立时激活的!
### **线程同步**
多线程的优势在于可以同时运行多个任务(至少感觉起来是这样)。但是当线程需要共享数据时,可能存在数据不同步的问题。
使用 Thread 对象的 Lock 和 Rlock 可以实现简单的线程同步,这两个对象都有 acquire 方法和 release 方法,对于那些需要每次只允许一个线程操作的数据,可以将其操作放到 acquire 和 release 方法之间。
import threading, time
更多Python视频、源码、资料加群683380553免费获取
创建锁
threadLock = threading.Lock()
创建线程列表
threads = []
class myThread(threading.Thread):
def init(self, threadID, name, counter):
threading.Thread.init(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print("Thread Start: " + self.name)
# 获取锁,同步线程
threadLock.acquire()
print_time(self.name, self.counter, 3)
# 释放锁,开启下一个线程
threadLock.release()
print("Thread Exit: " + self.name)
def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print(“{}: {}”.format(threadName, time.ctime()))
counter -= 1
创建线程
thread1 = myThread(1001, “Thread-1”, 1)
thread2 = myThread(1002, “Thread-2”, 2)
开启线程
thread1.start()
thread2.start()
添加线程列表
threads.append(thread1)
threads.append(thread2)
等待所有线程完成
for t in threads:
t.join()
print(“exit”)
E:\PyPro>python synchronize.py
Thread Start: Thread-1
Thread Start: Thread-2
Thread-1: Thu Apr 12 11:00:49 2018
Thread-1: Thu Apr 12 11:00:50 2018
Thread-1: Thu Apr 12 11:00:51 2018
Thread Exit: Thread-1
Thread-2: Thu Apr 12 11:00:53 2018
Thread-2: Thu Apr 12 11:00:55 2018
Thread-2: Thu Apr 12 11:00:57 2018
Thread Exit: Thread-2
exit
### **线程优先级队列**
Python 的 Queue 模块中提供了同步的、线程安全的队列类,包括FIFO队列Queue,LIFO队列LifoQueue,和优先级队列 PriorityQueue。
这些队列都实现了锁原语,能够在多线程中直接使用,可以使用队列来实现线程间的同步。
Queue 模块中的常用方法:
* Queue.qsize() 返回队列的大小
* Queue.empty() 如果队列为空,返回True,反之False
* Queue.full() 如果队列满了,返回True,反之False
* Queue.full 与 maxsize 大小对应
* Queue.get([block[, timeout]])获取队列,timeout等待时间
* Queue.get\_nowait() 相当Queue.get(False)
* Queue.put(item) 写入队列,timeout等待时间
* Queue.put\_nowait(item) 相当Queue.put(item, False)
* Queue.task\_done() 在完成一项工作之后,Queue.task\_done()函数向任务已经完成的队列发送一个信号
* Queue.join() 实际上意味着等到队列为空,再执行别的操作
import queue, threading, time
exitFlag = 0
更多Python视频、源码、资料加群683380553免费获取
创建锁
queueLock = threading.Lock()
创建队列
workQueue = queue.Queue(10)
class myThread(threading.Thread):
def init(self, threadID, name, q):
threading.Thread.init(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print("Thread Start: " + self.name)
process_data(self.name, self.q)
print("Thread Exit: " + self.name)
def process_data(threadName, q):
while not exitFlag:
queueLock.acquire()
if not workQueue.empty():
data = q.get()
queueLock.release()
print(“{} processing {}”.format(threadName, data))
else:
queueLock.release()
time.sleep(1)
threadList = [“Thread-1”, “Thread-2”, “Thread-3”]
nameList = [“One”, “Two”, “Three”, “Four”, “Five”]
threads = []
threadID = 1
创建新线程
for tName in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1
填充队列
queueLock.acquire()
print(“队列填充中>>>>>>>>>>>>>>”)
time.sleep(1)
for word in nameList:
workQueue.put(word)
print(“队列填充完毕>>>>>>>>>>>>>>”)
queueLock.release()
等待队列清空
while not workQueue.empty():
pass
通知线程退出
exitFlag = 1
等待所有线程完成
for t in threads:
t.join()
print(“exit”)
E:\PyPro>python queueue.py
Thread Start: Thread-1
Thread Start: Thread-2
Thread Start: Thread-3
队列填充中>>>>>>>>>>>>>>
队列填充完毕>>>>>>>>>>>>>>
Thread-3 processing One
Thread-1 processing Two
Thread-2 processing Three
Thread-3 processing Four
Thread-1 processing Five
Thread Exit: Thread-2
Thread Exit: Thread-1
Thread Exit: Thread-3
exit
现在能在网上找到很多很多的学习资源,有免费的也有收费的,当我拿到1套比较全的学习资源之前,我并没着急去看第1节,我而是去审视这套资源是否值得学习,有时候也会去问一些学长的意见,如果可以之后,我会对这套学习资源做1个学习计划,我的学习计划主要包括规划图和学习进度表。
分享给大家这份我薅到的免费视频资料,质量还不错,大家可以跟着学习
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
如果你需要这些资料,可以添加V无偿获取:hxbc188 (备注666)
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
ddd3fe5aff.png)
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
如果你需要这些资料,可以添加V无偿获取:hxbc188 (备注666)
[外链图片转存中…(img-7JOLitlm-1713845433298)]
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!