python爬虫(多线程&锁&信号量)

本文通过三个具体示例介绍Python中多线程的应用,包括无锁多线程、使用锁的多线程以及利用信号量控制并发数量的方法。每个示例都详细展示了如何启动线程并执行指定任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1、多线程(无锁):threading.Thread(target = ***).start()

  1 import threading
  2 import time
  3 import random
  4 
  5 def worker_func():
  6         print('worker thread is started at %s' % threading.current_thread())
  7         random.seed()
  8         time.sleep(random.random())
  9         print('worker thread is finished at %s' % threading.current_thread())
 10 
 11 def simple_thread_demo():
 12         for i in range(10):
 13                 threading.Thread(target=worker_func).start()
 14 
 15 if __name__ == '__main__':
 16         simple_thread_demo()


2、多线程(锁 & 单线程):<1> threading.Lock() <2> threading.Thread(target = ***,args = [***]).start()

  1 #coding=utf-8
  2 import threading
  3 import random
  4 import time
  5 
  6 def worker_func():
  7         print('worker thread is started at %s' % threading.current_thread())
  8         random.seed()
  9         time.sleep(random.random())
 10         print('worker thread is finished at %s' % threading.current_thread())
 11 
 12 def worker_func_lock(mylock):
 13         mylock.acquire()
 14         worker_func()
 15         mylock.release()
 16 
 17 gLock = threading.Lock()        #创建线程锁
 18 
 19 def thread_lock_demo():
 20         for i in range(10):
 21                 threading.Thread(target=worker_func_lock,args=[gLock]).start()  #开启线程
 22 
 23 if __name__ == '__main__':
 24         thread_lock_demo()


3、多线程(信号量 & 多线程):<1> threading.Semaphore() <2> threading.Thread(target = ***,args = [***]).start()

  1 #coding=utf-8
  2 import threading
  3 import random
  4 import time
  5 
  6 def worker_func():
  7         print('worker thread is started at %s' % threading.current_thread())
  8         random.seed()
  9         time.sleep(random.random())
 10         print('worker thread is finished at %s' % threading.current_thread())
 11 
 12 def worker_func_lock(mylock):
 13         mylock.acquire()
 14         worker_func()
 15         mylock.release()
 16 
 17 gSem = threading.Semaphore(3)   #信号量为3
 18 
 19 def thread_lock_demo():
 20         for i in range(10):
 21                 threading.Thread(target=worker_func_lock,args=[gSem]).start()   #开启线程
 22 
 23 if __name__ == '__main__':
 24         thread_lock_demo()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值