本文整理汇总了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')
示例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
示例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')
示例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
示例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()
示例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
示例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" )