当前位置: 首页>>代码示例>>Python>>正文


Python Timeout.start_new方法代码示例

本文整理汇总了Python中gevent.Timeout.start_new方法的典型用法代码示例。如果您正苦于以下问题:Python Timeout.start_new方法的具体用法?Python Timeout.start_new怎么用?Python Timeout.start_new使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在gevent.Timeout的用法示例。


在下文中一共展示了Timeout.start_new方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: foo3

# 需要导入模块: from gevent import Timeout [as 别名]
# 或者: from gevent.Timeout import start_new [as 别名]
def foo3():
    # 对各种Greenlet和数据结构相关的调用,gevent也提供了超时参数
    def wait():
        gevent.sleep(2)
        
    timer = Timeout(5).start()  # 5s没跑完就抛出Timeout异常
    thread1 = gevent.spawn(wait)
    
    try:
        thread1.join(timeout=timer)
    except Timeout:
        print('Thread 1 timed out')
    else:
        print('Thread 1 complete')
    
    timer = Timeout.start_new(1)
    thread2 = gevent.spawn(wait)
    
    try:
        thread2.get(timeout=timer)
    except Timeout:
        print('Thread 2 timed out')
        
    try:
        gevent.with_timeout(1, wait)
    except Timeout:
        print('Thread 3 timed out')
开发者ID:Rockyzsu,项目名称:base_function,代码行数:29,代码来源:09_2程序超时.py

示例2: wait

# 需要导入模块: from gevent import Timeout [as 别名]
# 或者: from gevent.Timeout import start_new [as 别名]
 def wait(self, timeout=None):
     if self.ready():
         return self.value
     else:
         switch = getcurrent().switch
         self.rawlink(switch)
         try:
             timer = Timeout.start_new(timeout) if timeout is not None else None
             try:
                 getattr(getcurrent(), 'awaiting_batch', lambda: None)()
                 result = get_hub().switch()
                 assert result is self, 'Invalid switch into AsyncResult.wait(): %r' % (result, )
             finally:
                 if timer is not None:
                     timer.cancel()
         except Timeout as exc:
             self.unlink(switch)
             if exc is not timer:
                 raise
         except:
             self.unlink(switch)
             raise
         # not calling unlink() in non-exception case, because if switch()
         # finished normally, link was already removed in _notify_links
     return self.value
开发者ID:flxf,项目名称:gbatchy,代码行数:27,代码来源:context.py

示例3: main

# 需要导入模块: from gevent import Timeout [as 别名]
# 或者: from gevent.Timeout import start_new [as 别名]
def main():
	timer = Timeout(1).start()
	thread1 = gevent.spawn(wait)
	
	try:
		thread1.join(timeout = timer)
	except Timeout:
		print('Thread1 timed out')
	
	# --
	
	timer = Timeout.start_new(1)
	thread2 = gevent.spawn(wait)
	
	try: 
		thread2.get(timeout = timer)
	except Timeout:
		print('thread2 timedout')
		
	# --
	
	try:
		gevent.with_timeout(1, wait)
	except Timeout:
		print('thread 3 timeout')
开发者ID:18965050,项目名称:gevent_examples,代码行数:27,代码来源:g11_setting_timeout.py

示例4: tpump

# 需要导入模块: from gevent import Timeout [as 别名]
# 或者: from gevent.Timeout import start_new [as 别名]
def tpump(input, output, chunk_size):
    size = 0
    while True:
        try:
            timeout = Timeout.start_new(5)
            chunk = input.read(chunk_size)
            timeout.cancel()
        except Timeout:
            input.release_conn()
            raise

        if not chunk: break
        output.write(chunk)
        size += len(chunk)
    return size
开发者ID:apendleton,项目名称:regulations-scraper,代码行数:17,代码来源:transfer.py

示例5: sample_addition

# 需要导入模块: from gevent import Timeout [as 别名]
# 或者: from gevent.Timeout import start_new [as 别名]
def sample_addition():
    timer = Timeout(1).start()
    thread1 = gevent.spawn(wait_2sec)

    try:
        thread1.join(timeout=timer)
    except Timeout:
        print 'Thread 1 time out'

    timer = Timeout.start_new(1)
    thread2 = gevent.spawn(wait_2sec)
    try:
        thread2.get(timeout=timer)
    except Timeout:
        print 'Thread 2 time out'

    try:
        gevent.with_timeout(1, wait_2sec)
    except Timeout:
        print 'Thread 3 time out'

    gevent.spawn(wait_2sec).get()
开发者ID:vhnuuh,项目名称:pyutil,代码行数:24,代码来源:timeout.py

示例6: join

# 需要导入模块: from gevent import Timeout [as 别名]
# 或者: from gevent.Timeout import start_new [as 别名]
    def join(self, timeout=None):
        """Wait until the greenlet finishes or *timeout* expires.
        Return ``None`` regardless.
        """
        if self.ready():
            return

        switch = getcurrent().switch
        self.rawlink(switch)
        try:
            t = Timeout.start_new(timeout)
            try:
                getattr(getcurrent(), 'awaiting_batch', lambda: None)()
                result = self.parent.switch()
                assert result is self, 'Invalid switch into Greenlet.join(): %r' % (result, )
            finally:
                t.cancel()
        except Timeout as ex:
            self.unlink(switch)
            if ex is not t:
                raise
        except:
            self.unlink(switch)
            raise
开发者ID:flxf,项目名称:gbatchy,代码行数:26,代码来源:context.py

示例7: wait

# 需要导入模块: from gevent import Timeout [as 别名]
# 或者: from gevent.Timeout import start_new [as 别名]
from gevent import Timeout

def wait( ):
	gevent.sleep( 2 )

timer = Timeout( 1 ).start()
thread1 = gevent.spawn( wait )

try:
	thread1.join( timeout=timer )
except Timeout:
	print( "Thread 1 timed out" )

# --

timer = Timeout.start_new( 1 )
thread2 = gevent.spawn( wait )

try:
	thread2.get( timeout=timer )
except Timeout:
	print( "Thread 2 timed out" )

# --

try:
	gevent.with_timeout( 1, wait )
except Timeout:
	print( "Thread 3 timed out" )

开发者ID:jsam,项目名称:Gevent-labs,代码行数:31,代码来源:lab10-timeouts-variety.py


注:本文中的gevent.Timeout.start_new方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。