python3 threading exercise

threading exercise
1、lock
官方描述:

	 A lock is not owned by the thread that locked it; another thread may
	 unlock it.  A thread attempting to lock a lock that it has already locked
	 will block until another thread unlocks it.  Deadlocks may ensue.

实例:

import threading as th
import _thread

n = 1
lock = th.Lock()

def task1():
    global n
    lock.acquire()
    for i in range(10):  
        print(lock.locked(),'locked status')
        n+=1
        print(n,'task1')
    print(lock.locked(),'locked status')
    lock.release()
    print(lock.locked(),'locked status')

	def task2():
	    global n
	    print(lock.locked(),'locked status222')
	    lock.acquire()
	    for i in range(10):
	        
	        print(lock.locked(),'locked status222')
	        n+=10
	        print(n,'task2')

	thread1 = th.Thread(target=task1)
	thread1.setName('task1')
	print(thread1.isDaemon(),thread1,thread1.name)
	thread2 = th.Thread(target=task2)
	thread2.setName('task2')
	print(thread2.isDaemon(),thread2,thread2.name)
	thread1.start()
	thread1.join(0.0000001)
	thread2.start()

daemon为boolean参数,添加守护进程,会导致无法请求锁

thread2 = th.Thread(target=task2,daemon=True)#初始化设置

thread2.setDaemon(daemonic)#之后设置,需传输daemonic参数(boolean)

守护进程小知识
守护进程是一个在后台运行并且不受任何终端控制的进程。Unix操作系统有很多典型的守护进程(其数目根据需要或20—50不等),它们在后台运行,执行不同的管理任务。
用户使守护进程独立于所有终端是因为,在守护进程从一个终端启动的情况下,这同一个终端可能被其他的用户使用。例如,用户从一个终端启动守护进程后退出,然后另外一个人也登录到这个终端。用户不希望后者在使用该终端的过程中,接收到守护进程的任何错误信息。同样,由终端键入的任何信号(例如中断信号)也不应该影响先前在该终端启动的任何守护进程的运行。虽然让服务器后台运行很容易(只要shell命令行以&结尾即可),但用户还应该做些工作,让程序本身能够自动进入后台,且不依赖于任何终端。
守护进程没有控制终端,因此当某些情况发生时,不管是一般的报告性信息,还是需由管理员处理的紧急信息,都需要以某种方式输出。Syslog 函数就是输出这些信息的标准方法,它把信息发送给 syslogd 守护进程

2、RLock
Rlock全称reentrant lock ,即可重复进入的锁,官方解释:

A reentrant lock must be released by the thread that acquired it. Once a
thread has acquired a reentrant lock, the same thread may acquire it again
without blocking; the thread must release it once for each time it has
acquired it.

已获取锁的线程可以重复进入,并支持再次调用acquire,同时只能由获取锁的线程解锁,其他线程无解锁权限。

实例:

import threading as th
import _thread

n = 1
lock = th.RLock()

#Lock

def task1():
    global n
    
    for i in range(10): 
        lock.acquire()  
        n+=1
        print(n,'task1')
        lock.release()
    
    
    

def task2():
    global n
    lock.acquire()
    for i in range(10):   
        n+=10
        print(n,'task2')

thread1 = th.Thread(target=task1)
thread1.setName('task1')
print(thread1.isDaemon(),thread1,thread1.name)
thread2 = th.Thread(target=task2)
thread2.setName('task2')
print(thread2.isDaemon(),thread2,thread2.name)

thread1.start()
thread1.join(0.0000001)
thread2.start()

3、Semaphore
设定请求锁的次数,超过则block,官方描述:

Semaphores manage a counter representing the number of release() calls minus
the number of acquire() calls, plus an initial value. The acquire() method
blocks if necessary until it can return without making the counter
negative. If not given, value defaults to 1.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值