Last active
September 18, 2019 11:27
-
-
Save matpalm/91db56b5de4cf374240725c439fc4221 to your computer and use it in GitHub Desktop.
sync_test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <thread> | |
using namespace std; | |
using namespace std::chrono; | |
// do all operations in milliseconds | |
int main(int argc, char *argv[]) { | |
// decide based on current epoch time when was the last epoch time | |
// divisible by 10? | |
auto now = time_point_cast<milliseconds>(system_clock::now()); | |
auto epoch_ms = now.time_since_epoch().count(); // long long | |
auto boundary_ms = epoch_ms / 10000 * 10000; // o_O oh boy... | |
// declare next checkpointing time to be this boundary + 10sec | |
auto checkpoint_ms = boundary_ms + 10000; | |
while (true) { | |
// how much do we need to sleep until the checkpoint? | |
auto now = time_point_cast<milliseconds>(system_clock::now()); | |
auto epoch_ms = now.time_since_epoch().count(); | |
auto sleep_ms_required = checkpoint_ms - epoch_ms; | |
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms_required)); | |
// do the thing | |
cout << "RUN" << endl; | |
// push checkpoint forward 10s | |
checkpoint_ms += 10000; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import time | |
# decide based on current epoch time when was the last epoch time | |
# divisible by 10. recall: python epoch time in seconds is a float | |
now = time.time() | |
boundary_sec = float(int(now/10)*10) | |
# declare next checkpointing time to be this boundary + 10 sec | |
checkpoint_sec = boundary_sec + 10 | |
while True: | |
# how much do we need to sleep until the checkpoint | |
now = time.time() | |
sleep_sec = checkpoint_sec - now | |
time.sleep(sleep_sec) | |
# do the thing | |
print("RUN") | |
# push checkpoint forward 10 sec | |
checkpoint_sec += 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment