Home > Backend Development > Python Tutorial > What's going on when tornado reports a TypeError (\'Unsupported timeout %r\' % timeout) error?

What's going on when tornado reports a TypeError (\'Unsupported timeout %r\' % timeout) error?

WBOY
Release: 2024-03-01 11:20:03
forward
948 people have browsed it

Whats going on when tornado reports a TypeError (\Unsupported timeout %r\ % timeout) error?

The reason for the error

This error is because the timeout parameter set is not supported when tornado is used in python. Normally, the value of the timeout parameter should be an integer or a float, but in this case an unsupported value was passed in. May be another type or a value that cannot be converted to an integer or float.

How to solve

To solve this error, you need to ensure that the value of the timeout parameter passed in is an integer or float. This can be ensured by adjusting code or configuration files. You can first check whether the value passed in the timeout parameter is legal. If it is not an integer or float, you can use functions such as int() or float() to convert it to a legal value.

Before you use the timeout parameter, you can also check whether the timeout parameter is legal. If not, set a default value or throw an exception.

Usage example

Yes, here is a simple example:

import tornado.ioloop

def handle_timeout():
print("timeout occurred")

def start_timeout(timeout):
if not isinstance(timeout, (int,float)):
raise ValueError("timeout must be a number")
tornado.ioloop.IOLoop.current().call_later(timeout, handle_timeout)

try:
start_timeout(10) # this will work
start_timeout("10") # this will raise ValueError
except ValueError as e:
print(str(e))
Copy after login

In this example, we check whether the timeout parameter passed in is an integer or float. If not, a ValueError exception will be thrown.

Another way is to use the default value, such as

def start_timeout(timeout=10):
if not isinstance(timeout, (int,float)):
timeout = 10 
tornado.ioloop.IOLoop.current().call_later(timeout, handle_timeout)

start_timeout() # this will use the default timeout of 10s
Copy after login

Here, we use a default value of 10s. If the timeout parameter passed in is illegal, we use the default value.

The above is the detailed content of What's going on when tornado reports a TypeError (\'Unsupported timeout %r\' % timeout) error?. For more information, please follow other related articles on the PHP Chinese website!

source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template