1

I want to write a Python program that waits some arbitrary time into the future (e.g. the time 500 milliseconds from now) and do something. I have a C++ code that does this fine using the Chrono library, so how can I do this in Python? I need accuracy of higher than 50 milliseconds (important) which this C++ code provides.


I do not want time.sleep(), I want a specific time_point in the future to sleep to and not a duration because I want to sync multiple threads to the same time.

int main() {
        std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();

      
        // get time 500 ms from now
        steady_clock::time_point next_frame = steady_clock::now() + std::chrono::milliseconds(500);

        std::thread t1{takeImages, next_frame};
        std::thread t2{takeImages, next_frame};

}

void takeImages(steady_clock::time_point next_frame)
{
    
        std::this_thread::sleep_until(next_frame);
        //do something here

}
Laurel
  • 5,965
  • 14
  • 31
  • 57
Lightsout
  • 3,454
  • 2
  • 36
  • 65
  • 1
    Voted to reopen because sleeping until a time point is not the same as sleeping for a time duration. We (humanity) have known this for several decades. For example the Ada programming language has long acknowledged and supported this distinction. A correct answer would acknowledge that python does not natively support sleeping until a time point, but 3rd party python libraries might help. – Howard Hinnant Oct 16 '19 at 03:28
  • The linked duplicate answer has nothing to do with the question. – Gerhard Oct 16 '19 at 07:14
  • Find an answer here: https://stackoverflow.com/questions/2031111/in-python-how-can-i-put-a-thread-to-sleep-until-a-specific-time – Gerhard Oct 16 '19 at 07:14
  • Thanks @Gerhard! _That's_ a duplicate! :-) I tried to cancel my vote to reopen but there does not seem to be a way to do that. – Howard Hinnant Oct 16 '19 at 13:28
  • @Gerhard I saw that answer but it seems like OP doesn't need <50ms accuracy like me and also seems like the top answer doesn't have anything close to that accuracy.. – Lightsout Oct 18 '19 at 06:03

0 Answers0