python3线程加锁的一个例子

再这个例子中,线程加上锁之后有并行变成了实际上的串行
[root@linux1 python_tidb]# cat mypy02.py
#!/usr/bin/env python3
import pymysql,sys,os
import _thread,time

def counter(myid,count):
    mutex.acquire()
    for i in range(count):
        time.sleep(1)
        #mutex.acquire()
        print("%s=>%s" % (myid,i))
        #mutex.release()
    mutex.release()

mutex = _thread.allocate_lock()
def parent():
    for i in range(5):
        myid = 'myid'+str(i)
        _thread.start_new_thread(counter,(myid,5,))

parent()

time.sleep(30)
[root@linux1 python_tidb]# ./mypy02.py
myid3=>0
myid3=>1
myid3=>2
myid3=>3
myid3=>4
myid4=>0
myid4=>1
myid4=>2
myid4=>3
myid4=>4
myid2=>0
myid2=>1
myid2=>2
myid2=>3
myid2=>4
myid1=>0
myid1=>1
myid1=>2
myid1=>3
myid1=>4
myid0=>0
myid0=>1
myid0=>2
myid0=>3
myid0=>4
[root@linux1 python_tidb]# 

线程加锁的另外一个例子

[root@linux1 python_tidb]# cat mypy02.py
#!/usr/bin/env python3
import pymysql,sys,os
import _thread,time,random

int1 = 0
def add_int1(myid,count):
    #mutex.acquire()
    global int1
    for i in range(count):
        time.sleep(random.random())
        if((i % 10)==0):
            print("%s=>%s" %(myid,int1))
        mutex.acquire()
        int1 +=  1
        mutex.release()
    #mutex.release()

mutex = _thread.allocate_lock()
def parent():
    for i in range(500):
        myid = 'myid'+str(i)
        _thread.start_new_thread(add_int1,(myid,100,))

parent()

time.sleep(100)
print("int1:: %s" %(int1))

如果不在函数里加这句 global int1,则会出现下面的错误

Unhandled exception in thread started by <function add_int1 at 0x7f5e60338bf8>
Traceback (most recent call last):
  File "./mypy02.py", line 14, in add_int1
    int1 +=  1
UnboundLocalError: local variable 'int1' referenced before assignment

另外一个我发现如果不让线程随机休眠time.sleep(random.random()),则很容易出现大家都在等待锁,而无法进行下去
我怀疑释放操作是不是和获取操作也互斥呢

最开始是 time.sleep(1)所有线程都sleep一秒的时候程序根本就进行不下去,谁也获得不了锁

并发申请锁是个问题

同步主线程和其它线程的一种实现方法,用锁

[root@linux1 python_tidb]# cat mypy02.py
#!/usr/bin/env python3
import pymysql,sys,os
import _thread,time,random

int1 = 0
def add_int1(myid,count):
    #mutex.acquire()
    global int1
    for i in range(count):
        time.sleep(random.random())
        if((i % 10)==0):
            print("%s=>%s" %(myid,int1))
        mutex.acquire()
        int1 +=  1
        mutex.release()
    #mutex.release()
    exit_mutexs[myid].release()

mutex = _thread.allocate_lock()
exit_mutexs =[_thread.allocate_lock() for i in range(500)]
for exit_mutex in exit_mutexs:
    exit_mutex.acquire()
def parent():
    for i in range(500):
        #myid = 'myid'+str(i)
        _thread.start_new_thread(add_int1,(i,100,))

parent()

for exit_mutex in exit_mutexs:
    while exit_mutex.locked():pass
print("int1:: %s" %(int1))

使用列表来同步主线程及其它线程

[root@linux1 python_tidb]# cat mypy02.py
#!/usr/bin/env python3
import pymysql,sys,os
import _thread,time,random

int1 = 0
def add_int1(myid,count):
    #mutex.acquire()
    global int1
    for i in range(count):
        time.sleep(random.random())
        if((i % 10)==0):
            print("%s=>%s" %(myid,int1))
        mutex.acquire()
        int1 +=  1
        mutex.release()
    #mutex.release()
    exit_status[myid]=True

mutex = _thread.allocate_lock()
#exit_mutexs =[_thread.allocate_lock() for i in range(500)]
#for exit_mutex in exit_mutexs:
#    exit_mutex.acquire()
exit_status = [False] * 500
def parent():
    for i in range(500):
        #myid = 'myid'+str(i)
        _thread.start_new_thread(add_int1,(i,100,))

parent()

#for exit_mutex in exit_mutexs:
#    while exit_mutex.locked():pass
while False in exit_status:pass
print("int1:: %s" %(int1))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值