线程和正则

#线程的五种状态  创建, 就绪, 运行,阻塞,死亡
'''import _thread
import time
#为线程定义一个函数
def a1(name,num):
    m=0
    while m<20:
        time.sleep(num)
        m+=1
        print(name,m)

try:
   _thread.start_new_thread(a1,("tom",1) )
   _thread.start_new_thread(a1,("marry",2))
except:
   print ("Error: 无法启动线程")
input("")
# 为线程定义一个函数'''
'''def print_time( threadName, delay):
   count = 0
   while count < 5:
      time.sleep(delay)
      count += 1
      print ("%s: %s" % ( threadName, time.ctime(time.time()) ))

# 创建两个线程
try:
   _thread.start_new_thread( print_time, ("Thread-1", 2, ) )
   _thread.start_new_thread( print_time, ("Thread-2", 1, ) )
except:
   print ("Error: 无法启动线程")
input("")'''
#类实现
import threading
class a1(threading.Thread):
    def run(self):
        for i in range(80):
            print(i)
s=a1()
m=a1()
s.start()
s.join()#加塞,

m.start()# s和m会并发执行,s加入join后s执行完再执行m ,里面可以加时间

#Condition   精确的控制锁 提供了四种方法
#acquire() 上锁  release() 解锁  wait()等待  notify() notify_all()唤醒
吃馒头例子:

import threading
import time
lock1=threading.Lock()
condHuoFu=threading.Condition(lock=lock1)
lock2=threading.Lock()
condChiHuo=threading.Condition(lock=lock2)
guo=[]
class HuoFu(threading.Thread):
    def run(self):
        #生产馒头
        while True:
            condChiHuo.acquire()
            if len(guo)==0:
                for i in range(1,11):
                    guo.append(i)
                    print("伙夫生产第%d个馒头"%i)
                    time.sleep(1)
                condChiHuo.notify_all()
            condChiHuo.release()
class ChiHuo(threading.Thread):
    mantou=None
    def __init__(self,name):
        threading.Thread.__init__(self)
        self.name=name
    def run(self):
        #吃馒头
        while True:
            condChiHuo.acquire()
            if len(guo)>0:
                mantou=guo.pop()
                time.sleep(1)
            else:
                #通知伙夫
                condHuoFu.acquire()
                condHuoFu.notify()
                condHuoFu.release()
                condChiHuo.wait()
            condChiHuo.release()
            if mantou is not None:
                print("{0}在吃第{1}个馒头".format(self.name,mantou))
lock1=threading.Lock()
condHuoFu=threading.Condition(lock1)#伙夫
lock2=threading.Lock()
condChiHuo=threading.Condition(lock2)#吃货
huofu=HuoFu()
huofu.start()
a1=ChiHuo("小明")
a2=ChiHuo("小王")
a3=ChiHuo("玛丽")
a1.start()
a2.start()
a3.start()
正则表达式

import re#导入模块
#re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,
# match()就返回none。
print(re.match('www', 'www.runoob.com').span())  # 在起始位置匹配
#输出结果是(0,3)
print(re.match('com', 'www.runoob.com'))  # 不在起始位置匹配
#输出结果是None
# .* 是贪婪模式    .*?是非贪婪模式
#正则表达式里的括号是分组的意思

line = "Cats are smarter than dogs"

matchObj = re.match(r'(.*) are (.*?) .*', line, re.M | re.I)

if matchObj:
    print("matchObj.group() : ", matchObj.group())#输出整个组
    #得到结果:Cats are smarter than dogs
    print("matchObj.group(1) : ", matchObj.group(1))#输出第一个组
    #得到结果:Cats
    print("matchObj.group(2) : ", matchObj.group(2))#输出第二个组
    #得到结果: smarter
else:
    print("No match!!")

# re.search()扫描整个字符串并返回第一个成功的匹配。
print(re.search('www', 'www.runoob.com').span())  # 在起始位置匹配
#结果是(0,3)
print(re.search('com', 'www.runoob.com').span())         # 不在起始位置匹配
#结果是(11,14)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值