线程,正则表达式

线程同步
同时对数据进行修改防止并发
锁使多线程任务更加安全
Condition:上锁(acquire())等待(wait())解锁(release())唤醒(notify(),notify_all())
创键锁:lock=threading.Lock()
        threading.Condition(lock=Lock)
[python]  view plain  copy
  1. import threading  
  2. import time  
  3. lock=threading.Lock()  
  4. cond=threading.Condition(lock=lock)  
  5. class Thread1(threading.Thread):  
  6.     def run(self):  
  7.         for i in range(1,11):  
  8.             if i==3:  
  9.                 cond.acquire()  
  10.                 cond.wait()  
  11.                 cond.release()  
  12.             print(i)  
  13.             time.sleep(1)  
  14. class Thread2(threading.Thread):  
  15.     def run(self):  
  16.         for i in range(30,19,-1):  
  17.             print(i)  
  18.             time.sleep(1)  
  19.         cond.acquire()  
  20.         cond.notify()  
  21.         cond.release()  
  22. t1=Thread1()  
  23. t2=Thread2()  
  24. t1.start()  
  25. t2.start()  
 
生产者和消费者模式
吃货与伙夫
[python]  view plain  copy
  1. import threading  
  2. import time  
  3. list=[]  
  4.   
  5. lock=threading.Lock()  
  6. huofuCon=threading.Condition(lock=lock)  
  7. lock2=threading.Lock()  
  8. chihuoCon=threading.Condition(lock=lock2)  
  9. class Huofu(threading.Thread):  
  10.     def run(self):  
  11.   
  12.         while True:  
  13.             chihuoCon.acquire()  
  14.             for i in range(1,11):  
  15.                 list.append(i)  
  16.                 print("伙夫生产第{0}个馒头".format(i))  
  17.                 time.sleep(1)  
  18.             #等待  
  19.             huofuCon.acquire()  
  20.             chihuoCon.notify_all()  
  21.             chihuoCon.release()  
  22.             huofuCon.wait()  
  23.             huofuCon.release()  
  24. mantou=None  
  25. class Chihuo(threading.Thread):  
  26.     def __init__(self,name):  
  27.         threading.Thread.__init__(self)  
  28.         self.name=name  
  29.     def run(self):  
  30.   
  31.         while True:  
  32.             chihuoCon.acquire()  
  33.             if len(list)==0:  
  34.                 huofuCon.acquire()  
  35.                 huofuCon.notify()  
  36.                 huofuCon.release()  
  37.                 chihuoCon.wait()  
  38.             chihuoCon.release()  
  39.             chihuoCon.acquire()  
  40.             if len(list)>0:  
  41.                 mantou=list.pop()  
  42.                 print("{0}在吃第{1}个馒头".format(threading.current_thread().name,mantou))  
  43.                 time.sleep(1)  
  44.             chihuoCon.release()  
  45. huofu=Huofu()  
  46. chihuo1=Chihuo('吃货1')  
  47. chihuo2=Chihuo('吃货2')  
  48. chihuo3=Chihuo('吃货3')  
  49. huofu.start()  
  50. chihuo1.start()  
  51. chihuo2.start()  
  52. chihuo3.start()  
正则表达式
re.M    多行匹配,影响 ^ 和 $
re.I 使匹配对大小写不敏感

[python]  view plain  copy
  1. #match  
  2. import re  
  3. print(re.match('www''www.runoob.com').span())  # 在起始位置匹配 返回0 3  
  4. print(re.match('com''www.runoob.com'))         # 不在起始位置匹配 返回None  
  5.   
  6.   
  7. line = "Cats are smarter than dogs"  
  8.   
  9. matchObj = re.match(r'(.*) are (.*?) .*', line, re.M | re.I)  
  10.   
  11. if matchObj:  
  12.     print("matchObj.group() : ", matchObj.group())  
  13.     print("matchObj.group(1) : ", matchObj.group(1))  
  14.     print("matchObj.group(2) : ", matchObj.group(2))  
  15. else:  
  16.     print("No match!!")  


[python]  view plain  copy
  1. #search  
  2. import re  
  3. print(re.search('www''www.runoob.com').span())  # 在起始位置匹配  
  4. print(re.search('com''www.runoob.com').span())         # 不在起始位置匹配  
  5.   
  6. import re  
  7. line = "Cats are smarter than dogs";  
  8.    
  9. searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I)  
  10.    
  11. if searchObj:  
  12.    print ("searchObj.group() : ", searchObj.group())  
  13.    print ("searchObj.group(1) : ", searchObj.group(1))  
  14.    print ("searchObj.group(2) : ", searchObj.group(2))  
  15. else:  
  16.    print ("Nothing found!!")  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值