Home >Backend Development >Python Tutorial >Solution to prompt TypeError(\'Unsupported deadline %r\' % deadline)

Solution to prompt TypeError(\'Unsupported deadline %r\' % deadline)

王林
王林forward
2024-03-01 13:20:45895browse

Solution to prompt TypeError(\Unsupported deadline %r\ % deadline)

The reason for the error

This error is caused by using Tornado’s ioLoop.add_timeout() or IOLoop.add_callback() method. The value of the deadline parameter passed is not an integer or floating point number. Numeric type parameters should be used.

How to solve

The solution is to check the value of the deadline parameter passed to the IOLoop.add_timeout() or IOLoop.add_callback() method in the code to make sure it is an integer or floating point number.

For example, if you are passing a stringor other non-numeric type value, convert it to an integer or float.

If the error is due to other reasons, please provide more context and code snippets to help me give you a more detailed answer.

Usage example

Yes, here is an example:

import tornado.ioloop

def my_callback():
print("callback called")

# This will work
deadline = tornado.ioloop.IOLoop.current().time() + 5.0
tornado.ioloop.IOLoop.current().add_timeout(deadline, my_callback)

# This will raise "TypeError("Unsupported deadline %r" % deadline)"
deadline = "5.0"
tornado.ioloop.IOLoop.current().add_timeout(deadline, my_callback)

In the first example, we are passing a floating point number as the deadline, which is correct. In the second example, we are passing a string, which will cause an error. We should convert deadline to float type.

deadline = float("5.0")
tornado.ioloop.IOLoop.current().add_timeout(deadline, my_callback)

This way you can avoid errors

If you want to run the callback function at some point in the future, you can use the IOLoop.add_timeout() method, which accepts a timestamp as the first parameter, and The callback function is run after this timestamp is reached. For example, if you want to run a callback function after 5 seconds, you can use the following code:

import tornado.ioloop

def my_callback():
print("callback called")

deadline = tornado.ioloop.IOLoop.current().time() + 5.0
tornado.ioloop.IOLoop.current().add_timeout(deadline, my_callback)

# Start the IOLoop
tornado.ioloop.IOLoop.current().start()

If you want to run the callback function in the next event loop, you can use the IOLoop.add_callback() method.

import tornado.ioloop

def my_callback():
print("callback called")

# This will call the callback on the next iteration of the event loop
tornado.ioloop.IOLoop.current().add_callback(my_callback)

# Start the IOLoop
tornado.ioloop.IOLoop.current().start()

Another method is to use the IOLoop.call_later() method, which accepts a number of seconds as the first parameter and runs the callback function after the time has elapsed.

For example, if you want to run the callback function after 5 seconds, you can use the following code:

import tornado.ioloop

def my_callback():
print("callback called")

tornado.ioloop.IOLoop.current().call_later(5, my_callback)

# Start the IOLoop
tornado.ioloop.IOLoop.current().start()

If you want to use these methods in more advanced scenarios, you can find more information in the Tornado documentation.

The above is the detailed content of Solution to prompt TypeError('Unsupported deadline %r' % deadline). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete