if m.group(3)!=None:

Start OpenStack Services

[python]
g=gen() 
print g   #<generator object gen at 0x02801760>  
print isinstance(g,types.GeneratorType) #True 

 

OpenStack Services After launching your
stack by Devstack, you maybe stop some services or reboot your machine.
This script help you start nova,keysto #! /bin/bash
###########…

   生成器对象支持多少个点子,如gen.next() ,gen.send() ,gen.throw()等。

        print ‘mail address or phone number is wrong’

After launching your stack by Devstack, you maybe stop some services or
reboot your machine.

看上边一段代码:

移动电话和一向电话在来电展现中平日出现‘+86’,所以匹配进度中要潜心这点

#! /bin/bash   
###########################  
# start OpenStack Services  
###########################  
# Help  
# this script is used to start several OpenStack Services after creating   
# devstack. Typically, run it after restarting machine.  
#constants  


#functions  


#call nohup  
function call_async(){  
 nohup $* &  
}  
#start keystone  
function start_keystone() {  
    echo "start keystone"  
    call_async python /opt/stack/keystone/bin/keystone-all \  
        --config-file /etc/keystone/keystone.conf \  
        --log-config /etc/keystone/logging.conf -d \  
        --debug \  
        > /tmp/start_keystone.log 2>&1 &  
}  


#glance  
function start_glance {  
    echo "start glance registry"  
    call_async python /usr/local/bin/glance-registry \  
        --config-file=/etc/glance/glance-registry.conf \  
        > /tmp/start_glance_reg.log 2>&1 &  
    echo "start glance api"  
    call_async python /usr/local/bin/glance-api \  
        --config-file=/etc/glance/glance-api.conf \  
        > /tmp/start_glance_api.log 2>&1 &  


}  


#nova  
function start_nova {   
    echo "start nova api"  
    call_async python /usr/local/bin/nova-api \  
        > /tmp/start_nova_api.log 2>&1 &  
    echo "start nova conductor"  
    call_async python /usr/local/bin/nova-conductor \  
        > /tmp/start_nova_conductor.log 2>&1 &  
    echo "start nova compute"  
    call_async python /usr/local/bin/nova-compute \  
        --config-file /etc/nova/nova.conf \  
        > /tmp/start_nova_compute.log 2>&1 &  
    echo "start nova cert"  
    call_async python /usr/local/bin/nova-cert \  
        > /tmp/start_nova_cert.log 2>&1 &  
    echo "start nova network"  
    call_async python /usr/local/bin/nova-network \  
        --config-file /etc/nova/nova.conf \  
        > /tmp/start_nova_network.log 2>&1 &  
    echo "start nova scheduler"  
    call_async python /usr/local/bin/nova-scheduler \  
        --config-file /etc/nova/nova.conf \  
        > /tmp/start_nova_scheduler.log 2>&1 &  
    echo "start nova novncproxy"  
    call_async python /usr/local/bin/nova-novncproxy \  
        --config-file /etc/nova/nova.conf \  
        --web /opt/stack/noVNC \  
        > /tmp/start_nova_novncproxy.log 2>&1 &  
    echo "start nova xvpvncproxy"  
    call_async python /usr/local/bin/nova-xvpvncproxy \  
        --config-file /etc/nova/nova.conf \  
        > /tmp/start_nova_vncproxy.log 2>&1 &  
    echo "start nova consoleauth"  
    call_async python /usr/local/bin/nova-consoleauth \  
        > /tmp/start_nova_noconsole.log 2>&1 &  
    echo "start nova objectstore"  
    call_async python /usr/local/bin/nova-objectstore \  
        > /tmp/start_nova_obj.log 2>&1 &  
}  


#cinder  
function start_cinder {  
    echo "start cinder api"  
    call_async python /usr/local/bin/cinder-api \  
        --config-file /etc/cinder/cinder.conf \  
        > /tmp/start_cinder_api.log 2>&1 &  
    echo "start cinder scheduler"  
    call_async python /usr/local/bin/cinder-scheduler \  
        --config-file /etc/cinder/cinder.conf \  
        > /tmp/start_cinder_scheduler.log 2>&1 &  
    echo "start cinder volume"  
    call_async python /usr/local/bin/cinder-volume \  
        --config-file /etc/cinder/cinder.conf \  
        > /tmp/start_cinder_volume.log 2>&1 &  
}  
#heat  
function start_heat {  
    echo "start heat engine"  
    call_async python /usr/local/bin/heat-engine \  
        --config-file=/etc/heat/heat-engine.conf \  
        > /tmp/start_heat_engine.log 2>&1 &  
    echo "start heat api"  
    call_async python /usr/local/bin/heat-api \  
        --config-dir=/etc/heat/heat-api.conf \  
        > /tmp/start_heat_api.log 2>&1 &  
    echo "start heat api cfn"  
    call_async python /usr/local/bin/heat-api-cfn \  
        --config-dir=/etc/heat/heat-api-cfn.conf \  
        > /tmp/start_heat_api_cfn.log 2>&1 &  
    echo "start heat api cloudwatch"  
    call_async python /usr/local/bin/heat-api-cloudwatch \  
        --config-dir=/etc/heat/heat-api-cloudwatch.conf \  
        > /tmp/start_heat_cw.log 2>&1 &  
}  




#main  
[ -z "${BASH_SOURCE[0]}" -o "${BASH_SOURCE[0]}" = "$0" ] || return  


echo "clean logs"  
sudo rm /tmp/start_*.log  


start_keystone  


# make sure the keystone is started.  
sleep 5  


start_glance  


start_cinder  


sleep 10  


start_nova  


sleep 10  


start_heat  

[python]
def stop_immediately(name): 
    if name == ‘skycrab’: 
        yield ‘okok’ 
    else: 
        print ‘nono’ 
 
s=stop_immediately(‘sky’) 
s.next() 

    if m==None:

ne,heat,cinder and glance.

    
这里有须求说一下multitask模块(不是规范库中的),看一段multitask使用的简练代码:

   
s.append(‘[email protected]’)

 

     只要函数中包罗yield关键字,该函数调用就是生成器对象。

    

This script help you start nova,keysto

      到了那边,next方法大家都懂了,下边看看send方法。

 

nono
Traceback (most recent call last):
  File “F:\python workspace\Pytest\src\cs.py”, line 170, in
<module>
    s.next()
StopIteration
      看上边代码,理解throw方法,throw首假如向生成器发送相当。

移动:134、135、136、137、138、139、147、150、152、154、157、158、159、182、183、187、188

    
精通了这一个,大家就能够向协同程序发起攻击了,所谓协同程序也正是是足以挂起,苏醒,有三个步向点。其实轻易,也正是说多个函数能够同偶尔候扩充,能够相互发送新闻等。

    for i in range(len(s)):

print g.next() #None  1    
再调用next方法,你大概会好奇,为什么打字与印刷出七个值,不急,且听本身渐渐道来。

                print ‘It is %s mail,the address is %s’
%(m.group(2),m.group())

tmp的值为None,此时出口None,并施行到下贰遍yield x,所以又输出1.

该代码中正则表明式分为三有的:

t=multitask.TaskManager()
t.add(tt())
t.add(gg())
t.run()
结果:

It is qq mail,the address is
[email protected]

def mygen():
    try:
        yield ‘something’
    except ValueError:
        yield ‘value error’
    finally:
        print ‘clean’  #一定会被推行
gg=mygen()
print gg.next() #something
print gg.throw(ValueError) #value error  clean
     调用gg.next很分明此时出口‘something’,并在yield
‘something’暂停,此时向gg发送ValueError卓殊,恢复生机实践意况,except 
将会捕捉,并出口新闻。

 

  
假设只是是兑现地点的效劳,其实很简短,大家能够和煦写四个。首要思路就是将生成器对象放入队列,推行send(None)后,若无抛出StopIteration,将该生成器对象再插手队列。

    else:        

生成器比迭代器更抓牢有力也进一步复…

2.中华三大运营商手机号段

t=Task()
t.add(tt())
t.add(gg())
t.run()

 

[python]
tt0 
xx0 
tt1 
xx1 
tt2 
xx2 
tt3 
xx3 

   
s.append(‘[email protected]’)

生成器比迭代器更抓牢有力也越加目不暇接,要求花点武术好好精通贯通。

It is tju mail,the address is
[email protected]

class Task():
    def __init__(self):
        self._queue = Queue.Queue()

 

print g.next() # 0   
调用生成器的next方法,将运转到yield地方,此时打退堂鼓施行情形,并回到yield后的值。所以打字与印刷出的是1,暂停实践情形。

p=re.compile(r’^[\w\d]+[\d\w\_\.][email protected]([\d\w]+)\.([\d\w]+)(?:\.[\d\w]+)?$|^(?:\+86)?(\d{3})\d{8}$|^(?:\+86)?(0\d{2,3})\d{7,8}$’)

    def run(self):
        while not self._queue.empty():
            for i in xrange(self._queue.qsize()):
                try:
                    gen= self._queue.get()
                    gen.send(None)
                except StopIteration:
                    pass
                else:
                    self._queue.put(gen)

(2) ^(?:\+86)?(0\d{2,3})\d{7,8}$ 这一部分用来相配固定电话

tt0
xx0
tt1
xx1
tt2
xx2
tt3
xx3  
假设不是行使生成器,那么要达成地方现象,即函数交错输出,那么只好选取线程了,所以生成器给大家提供了更广阔的前景。

    s.append(‘+8615822123456’)

    def add(self,gen):
        self._queue.put(gen)

if __name__==’__main__’:

[python]
print g.next() #None  1 

It is 163 mail,the address is
[email protected]

g=gen()
print g   #<generator object gen at 0x02801760>
print isinstance(g,types.GeneratorType) #True   
大家能够看来,gen()实际不是函数调用,而是产生生成器对象。

 

 
当然,multitask完成的终将不仅那几个职能,有意思味的童鞋能够看下源码,依旧相比较轻松易懂的。

 

[python]
def tt(): 
    for x in xrange(4): 
        print ‘tt’+str(x) 
        yield 
 
def gg(): 
    for x in xrange(4): 
        print ‘xx’+str(x) 
        yield 
 
t=multitask.TaskManager() 
t.add(tt()) 
t.add(gg()) 
t.run() 

 

[python]
print g.next() # 0 

发表评论

电子邮件地址不会被公开。 必填项已用*标注