📜  python超时异常 - Python代码示例

📅  最后修改于: 2022-03-11 14:47:15.098000             🧑  作者: Mango

代码示例1
--------------------------------------------------------------
Timeout function
--------------------------------------------------------------
import asyncio
from async_timeout import timeout

class Student:
      def __init__(self):
          self.queue = asyncio.Queue()
          pass
      
    async def function(self):
          try:
            async with timeout(300): # 5 minutes...
                  #source = await self.queue.get()
                
                #do what u need to do
                pass
                
        except asyncio.TimeoutError as e:
            print(e)
            
            
--------------------------------------------------------------
Requests
--------------------------------------------------------------
import requests as r

class Student:
    def __init__(self):
          pass
      
      def function(url:string):
        try:
            data = r.get(url, timeout=10.0)
        except requests.Timeout as err:
            logger.error({"message": err.message})
        except Exception as err:
              print(err)
        return data # or what ever u need to return