counter = 0 def process_item(item): global counter ... do something with item ... counter += 1
lock = Lock() lock.acquire() #: will block if lock is already held ... access shared resource lock.release()
lock.acquire() try: ... access shared resource finally: lock.release() #: release lock, no matter what
from __future__ import with_statement #: 2.5 only with lock: ... access shared resource
if not lock.acquire(False):
... 锁资源失败
else:
try:
... access shared resource
finally:
lock.release()
if not lock.locked(): #: 其它线程可能在下一条语句执行之前占有了该锁 lock.acquire() #: 可能会阻塞
lock = threading.Lock()
def get_first_part():
lock.acquire()
try:
... 从共享对象中获取第一部分数据
finally:
lock.release()
return data
def get_second_part():
lock.acquire()
try:
... 从共享对象中获取第二部分数据
finally:
lock.release()
return data
def get_both_parts(): first = get_first_part() seconde = get_second_part() return first, second
def get_both_parts():
lock.acquire()
try:
first = get_first_part()
seconde = get_second_part()
finally:
lock.release()
return first, second
lock = threading.Lock() lock.acquire() lock.acquire() #: 这里将会阻塞 lock = threading.RLock() lock.acquire() lock.acquire() #: 这里不会发生阻塞
lock = threading.RLock() def get_first_part(): ... see above def get_second_part(): ... see above def get_both_parts(): ... see above
semaphore = threading.BoundedSemaphore() semaphore.acquire() #: counter减小
semaphore.release() #: counter增大
max_connections = 10 semaphore = threading.BoundedSemaphore(max_connections)
event = threading.Event() #: 一个客户端线程等待flag被设定 event.wait() #: 服务端线程设置或者清除flag event.set() event.clear()
#: 表示一个资源的附属项
condition = threading.Condition()
生产者线程在通知消费者线程有新生成资源之前需要获得条件:
#: 生产者线程
... 生产资源项
condition.acquire()
... 将资源项添加到资源中
condition.notify() #: 发出有可用资源的信号
condition.release()
消费者必须获取条件(以及相关联的锁),然后尝试从资源中获取资源项:
#: 消费者线程
condition.acquire()
while True:
...从资源中获取资源项
if item:
break
condition.wait() #: 休眠,直至有新的资源
condition.release()
... 处理资源
lock = threading.RLock() condition_1 = threading.Condition(lock) condition_2 = threading.Condition(lock)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time, threading
# 假定这是你的银行存款:
balance = 0
muxlock = threading.Lock()
def change_it(n):
# 先存后取,结果应该为0:
global balance
balance = balance + n
balance = balance - n
def run_thread(n):
# 循环次数一旦多起来,最后的数字就变成非0
for i in range(100000):
change_it(n)
t1 = threading.Thread(target=run_thread, args=(5,))
t2 = threading.Thread(target=run_thread, args=(8,))
t3 = threading.Thread(target=run_thread, args=(9,))
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
print balance
[/data/web/test_python]$ python multhread_threading.py 0 [/data/web/test_python]$ python multhread_threading.py 61 [/data/web/test_python]$ python multhread_threading.py 0 [/data/web/test_python]$ python multhread_threading.py 24
#创建锁mutex = threading.Lock() #锁定mutex.acquire([timeout]) #释放mutex.release()
balance = 0
muxlock = threading.Lock()
def change_it(n):
# 获取锁,确保只有一个线程操作这个数
muxlock.acquire()
global balance
balance = balance + n
balance = balance - n
# 释放锁,给其他被阻塞的线程继续操作
muxlock.release()
def run_thread(n):
for i in range(10000):
change_it(n)
[/data/web/test_python]$ python multhread_threading.py 0 [/data/web/test_python]$ python multhread_threading.py 0 [/data/web/test_python]$ python multhread_threading.py 0 [/data/web/test_python]$ python multhread_threading.py 0
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有