Put the process to sleep for a certain period of time (seconds) or more in Python

Implementation when you want to stop processing for a certain period of time in Python.

When calling an external API many times in a for statement, you may want to leave a certain interval. But it's a little inefficient if you always put a certain amount of sleep. I want to put sleep if the processing time is less than a certain interval.

Example) I want to leave an interval of 2 seconds or more between requests to the Twitter API.

import time
from datetime import datetime


def hoge():
    start = datetime.now()

    #Here API request and related processing
    hoge()

    exec_time = (datetime.now() - start).microseconds

    #If the process does not take more than 2 seconds, sleep for a minimum of 2 seconds
    sleep_sec = float(2000 - exec_time) / 1000
    if sleep_sec > 0:
        time.sleep(sleep_sec)

There seems to be a better way to write it.

Recommended Posts

Put the process to sleep for a certain period of time (seconds) or more in Python
Randomly play the movie on ChromeCast for a certain period of time
Check the processing time and the number of calls for each process in python (cProfile)
How to use the Slack API using Python to delete messages that have passed a certain period of time for a specific user on a specific channel
How to check the memory size of a variable in Python
[Introduction to Python] How to use the in operator in a for statement?
How to check the memory size of a dictionary in Python
A function that measures the processing time of a method in python
[Python3] Define a decorator to measure the execution time of a function
The story of returning to the front line for the first time in 5 years and refactoring Python Django
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
MongoDB for the first time in Python
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
How to get the date and time difference in seconds with python
[Python] How to put any number of standard inputs in a list
How to format a list of dictionaries (or instances) well in Python
Get a datetime instance at any time of the day in Python
I made a program to check the size of a file in Python
A useful note when using Python for the first time in a while
Python: I want to measure the processing time of a function neatly
[Python] How to open two or more files at the same time
4 methods to count the number of occurrences of integers in a certain interval (including imos method) [Python implementation]
A story about trying to improve the testing process of a system written in C language for 20 years
Get the caller of a function in Python
Make a copy of the list in Python
A clever way to time processing in Python
To add a module to python put in Julialang
Output in the form of a python array
To represent date, time, time, and seconds in Python
Various ways to read the last line of a csv file in Python
How to pass the execution result of a shell command in a list in Python
Experiment to collect tweets for a long period of time (aggregation & content confirmation)
I searched for the skills needed to become a web engineer in Python
Python script to get a list of input examples for the AtCoder contest
Turn multiple lists with a for statement at the same time in Python
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to get the number of digits in Python
How to measure processing time in Python or Java
Register a task in cron for the first time
How to identify the element with the smallest number of characters in a Python list?
The concept of reference in Python collapsed for a moment, so I experimented a little.
How to unit test a function containing the current time using freezegun in python
How to check in Python if one of the elements of a list is in another list
Find a guideline for the number of processes / threads to set in the application server
Output the specified table of Oracle database in Python to Excel for each file
A story about trying to introduce Linter in the middle of a Python (Flask) project
A reminder about the implementation of recommendations in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Python Note: The mystery of assigning a variable to a variable
I made a program in Python that changes the 1-minute data of FX to an arbitrary time frame (1 hour frame, etc.)
How to change python version of Notebook in Watson Studio (or Cloud Pak for Data)
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
The story of creating a "spirit and time chat room" exclusively for engineers in the company
How to save memory when reading huge XML of several GB or more in Python
Get the value of a specific key up to the specified index in the dictionary list in Python
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
__init__ called by wxPython or Tkinter was a __init__ call of the inheriting class in Python
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
How to quickly count the frequency of appearance of characters from a character string in Python?
How to calculate the sum or average of time series csv data in an instant