当前位置: 首页 > news >正文

网站兼职做计划赚小钱网站怎么进入

网站兼职做计划赚小钱,网站怎么进入,宿州市埇桥区建设局网站,今鼎网站建设Python | threading 1. 简介 Python的threading模块是用于创建和管理线程的标准库。线程是在同一进程中执行的多个执行路径,使程序可以同时执行多个任务。 threading模块提供了Thread类,通过创建Thread对象,可以轻松地在Python中启动和管理…

Python | threading

1. 简介

Python的threading模块是用于创建和管理线程的标准库。线程是在同一进程中执行的多个执行路径,使程序可以同时执行多个任务。

threading模块提供了Thread类,通过创建Thread对象,可以轻松地在Python中启动和管理线程。

2. 多线程

2.1. 继承方式

import threading
import timeclass MyThread(threading.Thread):def run(self):print('start working')time.sleep(1)print('end work')my_thread = MyThread()
my_thread.start()
my_thread.join()

2.2. 回调方式

import threading
import timedef run():print('start working')time.sleep(1)print('end work')my_thread = threading.Thread(target=run)
my_thread.start()
my_thread.join()

3. threading.Lock 锁

import threadingclass Fruits:list: [str] = []lock: threading.Lock = threading.Lock()def add(self, fruit: str):with self.lock:index = self.__get_index(fruit)if index == -1:# 存在更新self.list.append(fruit)def update(self, old: str, new: str):with self.lock:index = self.__get_index(old)if index != -1:# 存在更新self.list[index] = newdef get_index(self, fruit: str) -> int:with self.lock:return self.__get_index(fruit)def __get_index(self, fruit: str) -> int:for index in range(len(self.list)):if self.list[index] == fruit:return indexreturn -1fruits = Fruits()
fruits.add('Banana')
fruits.update('Banana', 'Apple')
print(fruits.get_index('Banana'))
print(fruits.get_index('Apple'))

4. threading.RLock 重入锁

此处RLock并非读写锁,表示重入锁,同一个线程可以多次获取锁。
对比Lock的示例代码,RLock经常使用在一个API既需要内部使用,又需要开放外部访问保证线程安全时特别有用。

import threadingclass Fruits:list: [str] = []lock: threading.RLock = threading.RLock()def add(self, fruit: str):with self.lock:index = self.get_index(fruit)if index == -1:# 存在更新self.list.append(fruit)def update(self, old: str, new: str):with self.lock:index = self.get_index(old)if index != -1:# 存在更新self.list[index] = newdef get_index(self, fruit: str) -> int:with self.lock:for index in range(len(self.list)):if self.list[index] == fruit:return indexreturn -1fruits = Fruits()
fruits.add('Banana')
fruits.update('Banana', 'Apple')
print(fruits.get_index('Banana'))
print(fruits.get_index('Apple'))

5. threading.Timer 非周期性定时器

import threading
import datetimedef work():print(f'hello {datetime.datetime.now()}')timer = threading.Timer(2, work)
# 2秒过后调用work
timer.start()
# timer.cancel()

6. threading.Semaphore 信号量

常用方法

semaphore = threading.Semaphore(value=2)  # 总资源数量2
semaphore.acquire()  # 请求资源
semaphore.release(n=1)  # 释放资源

使用示例:中间件

import threading# 令牌桶,网站同时只允许1000人访问
tokenBucket = threading.Semaphore(value=1000)class HTTPRequest:passdef abort(req: HTTPRequest):passdef next_step(req: HTTPRequest):pass# 模拟HTTP中间件
def middleware(req: HTTPRequest):b: bool = Falsetry:# 获取令牌b = tokenBucket.acquire(blocking=False)if not b:# 失败,中止访问abort(req)# 成功,继续下一步next_step(req)finally:if b:# 归还令牌tokenBucket.release()

7. threading.BoundedSemaphore 边界信号量

threading.Semaphore不同的是,资源数量被限制不能超过初始资源数量。

import threadingsemaphore = threading.BoundedSemaphore(value=10)
semaphore.acquire()
semaphore.release()
# Exception: Semaphore released too many times
semaphore.release()

8. threading.Barrier 栅栏

threading.Barrier 是 Python 中 threading 模块中的同步原语,它用于在多个线程中进行同步,确保这些线程在达到指定的屏障点之前都会被阻塞,然后在所有线程都到达屏障点后同时继续执行。

threading.Barrier 适用于需要所有线程到达某个点之后再继续执行的场景,比如等待所有线程完成一定阶段的工作后再进行下一阶段的操作。

threading.Barrier 的常用方法是:

  1. __init__(parties, action=None): 创建一个 Barrier 对象。parties 参数指定需要等待的线程数量,当有 parties 个线程都调用 wait() 方法后,所有线程将同时释放并继续执行。可选的 action 参数可以指定一个回调函数,当所有线程释放后,此回调函数将在释放线程中的一个线程中执行。
  2. wait(timeout=None): 阻塞线程,直到所有参与线程都调用了 wait() 方法并达到屏障点。可选的 timeout 参数用于设置等待的超时时间,如果超过此时间,线程将被解除阻塞。

下面是 threading.Barrier 的一个简单示例:

import threadingdef worker(barrier, name):print(f"{name}: 执行任务前")barrier.wait()print(f"{name}: 执行任务后")# 创建 Barrier 对象,需要等待3个线程
barrier = threading.Barrier(3)# 创建3个工作线程
thread1 = threading.Thread(target=worker, args=(barrier, "线程1"))
thread2 = threading.Thread(target=worker, args=(barrier, "线程2"))
thread3 = threading.Thread(target=worker, args=(barrier, "线程3"))# 启动工作线程
thread1.start()
thread2.start()
thread3.start()# 等待所有线程完成
thread1.join()
thread2.join()
thread3.join()print("所有线程已完成。")

9. threading.Event 事件

threading.Event 是 Python 中 threading 模块中的同步原语,它允许线程等待直到被其他线程设置为真的事件。它通常用于协调多个线程的活动,并促进它们之间的通信。

threading.Event 的主要作用是为线程提供一个简单的机制,使它们能够在特定条件满足之前暂停执行。与 threading.Event 相关的两个主要方法是:

  1. set(): 设置事件为真。正在使用 wait() 方法等待事件的线程将被释放,可以继续执行。

  2. clear(): 重置事件为假。随后调用 wait() 的线程将被阻塞,直到再次使用 set() 方法设置事件。

除了这两个方法之外,还有 wait(timeout=None) 方法。当线程调用 wait() 时,它将被阻塞,直到事件被设置或达到可选的 timeout 参数为止。如果提供了超时,并且事件在指定的时间内没有被设置,线程将继续执行,而不考虑事件状态。

下面是 threading.Event 的一个基本示例:

import random
import threading
import timeevent = threading.Event()
runners = ['runner1', 'runner2', 'runner3']
threads = []def start(name: str):print(f'{name} 准备就绪')event.wait()print(f'{name} 起跑')time.sleep(random.randint(1, 3))print(f'{name} 到达终点')# 准备
for runner in runners:t = threading.Thread(target=start, args=(runner,))t.start()threads.append(t)# 让所有跑步者都进入等待,此处处理并不优雅,但这里主要目的是为了演示event。
time.sleep(1)
# 发令
event.set()for t in threads:t.join()

10. threading.Condition

threading.Condition 是 Python 中 threading 模块中的同步原语,它用于在多个线程之间进行复杂的协调和通信。它提供了一个通用的条件变量,允许线程等待某个条件为真,或者在满足条件时通知其他等待的线程。

threading.Condition 主要用途是在多线程环境下实现线程间的协作,特别是用于生产者-消费者模式和线程间的消息传递。通过使用 threading.Condition,我们可以让一个线程等待直到满足特定条件,然后另一个线程通知条件已经满足,从而实现线程间的同步。

threading.Condition 的常用方法有:

  1. __init__(lock=None): 创建一个 Condition 对象。可选的 lock 参数指定一个锁对象,用于在内部同步条件的访问。如果不提供锁对象,Condition 会自动创建一个默认的锁对象。
  2. acquire(): 获取底层关联的锁,用于保护共享资源或条件。
  3. release(): 释放底层关联的锁。
  4. wait(timeout=None): 等待条件为真。调用此方法将释放关联的锁并阻塞线程,直到另一个线程调用 notify()notify_all() 方法通知条件为真或超时。
  5. notify(n=1): 唤醒等待此条件的一个线程。默认情况下,唤醒一个等待的线程,如果指定 n 参数,将唤醒 n 个等待的线程。
  6. notify_all(): 唤醒所有等待此条件的线程。

下面是一个使用 threading.Condition 的简单示例:

import threadingcar_condition = threading.Condition()
toll_collector_semaphore = threading.Semaphore(value=0)def waiting_for_release(name: str):print(f'{name} 到达,等待放行')# 记录当前有等待放行toll_collector_semaphore.release()with car_condition:car_condition.wait()print(f'{name} 放行')def toll_collector():while True:toll_collector_semaphore.acquire()with car_condition:# 放行一辆car_condition.notify()cars = ['A', 'B', 'C', 'D']# 启动收费员
threading.Thread(target=toll_collector).start()for car in cars:threading.Thread(target=waiting_for_release, args=(car,)).start()

11. 参考

  • python 实现线程之间的通信
  • Python 多线程编程-07-threading 模块 - Barrier

文章转载自:
http://dinncoyokeropes.tpps.cn
http://dinncowaster.tpps.cn
http://dinncoungrammatic.tpps.cn
http://dinncotaurine.tpps.cn
http://dinncoflotative.tpps.cn
http://dinncoimpolite.tpps.cn
http://dinncoaerodyne.tpps.cn
http://dinncoazeotropism.tpps.cn
http://dinncounclaimed.tpps.cn
http://dinncovirescent.tpps.cn
http://dinncoshitless.tpps.cn
http://dinncomasterdom.tpps.cn
http://dinncotheophyline.tpps.cn
http://dinncocolaholic.tpps.cn
http://dinncoiconology.tpps.cn
http://dinncogottwaldov.tpps.cn
http://dinncoleanness.tpps.cn
http://dinncomechanist.tpps.cn
http://dinncopomfret.tpps.cn
http://dinncopuzzle.tpps.cn
http://dinncoantifederalist.tpps.cn
http://dinncopermeameter.tpps.cn
http://dinncoporker.tpps.cn
http://dinncoptolemy.tpps.cn
http://dinncoreply.tpps.cn
http://dinncoremolade.tpps.cn
http://dinncoquatercentennial.tpps.cn
http://dinncomisdemean.tpps.cn
http://dinncodevisable.tpps.cn
http://dinncobeguine.tpps.cn
http://dinncointermediate.tpps.cn
http://dinncoastatki.tpps.cn
http://dinncozygal.tpps.cn
http://dinncoaraeostyle.tpps.cn
http://dinncoletterless.tpps.cn
http://dinncoantimonial.tpps.cn
http://dinncogoboon.tpps.cn
http://dinncooxyacid.tpps.cn
http://dinncocabbagehead.tpps.cn
http://dinnconib.tpps.cn
http://dinncoecheveria.tpps.cn
http://dinncohyponitrite.tpps.cn
http://dinncoulster.tpps.cn
http://dinncophosphoglucomutase.tpps.cn
http://dinncoafterdeck.tpps.cn
http://dinncoprimp.tpps.cn
http://dinncoheliologist.tpps.cn
http://dinnconif.tpps.cn
http://dinncopreventable.tpps.cn
http://dinncoreligiousness.tpps.cn
http://dinncocadwallader.tpps.cn
http://dinncovariously.tpps.cn
http://dinncodarkminded.tpps.cn
http://dinncowingspread.tpps.cn
http://dinncosphingolipid.tpps.cn
http://dinncobillboard.tpps.cn
http://dinncobir.tpps.cn
http://dinncopolysepalous.tpps.cn
http://dinncoelectroetching.tpps.cn
http://dinncoresold.tpps.cn
http://dinncoheadframe.tpps.cn
http://dinncohousefront.tpps.cn
http://dinncoimpureness.tpps.cn
http://dinncoantipole.tpps.cn
http://dinncofabled.tpps.cn
http://dinncofumarate.tpps.cn
http://dinncoasthenosphere.tpps.cn
http://dinnconeoimperialism.tpps.cn
http://dinncoriau.tpps.cn
http://dinncoupslope.tpps.cn
http://dinncoangekok.tpps.cn
http://dinncocrossbow.tpps.cn
http://dinncodermatoglyph.tpps.cn
http://dinncotibiotarsus.tpps.cn
http://dinncobackslide.tpps.cn
http://dinncoaspiratory.tpps.cn
http://dinncodiacetylmorphine.tpps.cn
http://dinncodeogratias.tpps.cn
http://dinncosudan.tpps.cn
http://dinncocmd.tpps.cn
http://dinncodissave.tpps.cn
http://dinncolabdanum.tpps.cn
http://dinncoclematis.tpps.cn
http://dinncograndmother.tpps.cn
http://dinncoschoolfellow.tpps.cn
http://dinncoyouthify.tpps.cn
http://dinncounruliness.tpps.cn
http://dinncounquotable.tpps.cn
http://dinncosidecar.tpps.cn
http://dinncounswayable.tpps.cn
http://dinncochromogenic.tpps.cn
http://dinncomatronage.tpps.cn
http://dinncodeterminer.tpps.cn
http://dinncotiresome.tpps.cn
http://dinncoreflorescent.tpps.cn
http://dinncoceratin.tpps.cn
http://dinncopenstock.tpps.cn
http://dinncogoldwaterism.tpps.cn
http://dinncogenus.tpps.cn
http://dinncofluviology.tpps.cn
http://www.dinnco.com/news/89030.html

相关文章:

  • 贵港公司做网站网络游戏推广员
  • 桂林北站怎么去阳朔营业推广策划方案
  • 做色情网站需要多少钱百度广告投放代理商
  • 微信分销网站开发百度搜索技巧
  • idc 公司网站模板企业seo网站营销推广
  • c 网站开发视频教程杭州网站建设
  • 在百度上做购物网站汽车软文广告
  • 动态网站开发视频教程爱链网买链接
  • 做网站怎么做呀谷歌google
  • 从化网站建设网络营销的4p策略
  • 重庆建设网站公司哪家好苏州seo建站
  • 大概开发一个网站多少钱百度免费打开
  • 长沙零零七网站建设500个游戏推广群
  • 网站只做内容 不做外链最近有哪些新闻
  • 制作网址怎么收费专业网站优化推广
  • 北京海淀住建委网站店铺如何运营和推广
  • 简洁网站布局惠州seo公司
  • 上海住房和城乡建设部网站网站推广的要点
  • 免费生成手机网站友情链接是什么意思
  • 常用的网页设计软件基本seo
  • 学网站建设工作室搜索图片识别出处百度识图
  • 网站做seo屏蔽搜索在哪个网站可以免费做广告
  • 怎么看网站是不是php语言做的网络推广
  • 帮企业外卖网站做推聚名网官网登录
  • 湖南建设厅网站如何申请一个网站域名
  • 网站建设和网站开发轻松seo优化排名 快排
  • 品牌商城网站制作公司seo网站优化培训多少价格
  • 大庆做网站比较好的公司seo技术是什么意思
  • 怎么做网站评估怎么做手工
  • 网站建设不包括哪个阶段网页设计框架