Last active
February 11, 2018 02:58
-
-
Save kurogelee/887c8a3504ac9c904e929c156aedf8c9 to your computer and use it in GitHub Desktop.
Pythonで一定時間ごとに処理を実行する ref: https://qiita.com/kurogelee/items/0e5fd8b6a1d1f169179a
This file contains hidden or 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
import time | |
def worker(): | |
print(time.time()) | |
time.sleep(8) | |
interval = 5 | |
while True: | |
worker() | |
time.sleep(interval) |
This file contains hidden or 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
1518294594.6087666 | |
1518294607.611248 | |
1518294620.6149857 |
This file contains hidden or 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
>>> schedule(5, worker) | |
1518295406.8357077 | |
1518295416.849788 | |
1518295426.8451777 |
This file contains hidden or 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
>>> schedule(5, worker, False) | |
1518295478.5527058 | |
1518295483.5534766 | |
1518295488.5580828 |
This file contains hidden or 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
import time | |
import threading | |
def worker(): | |
print(time.time()) | |
time.sleep(8) | |
def schedule(interval, f, wait=True): | |
base_time = time.time() | |
next_time = 0 | |
while True: | |
t = threading.Thread(target=f) | |
t.start() | |
if wait: | |
t.join() | |
next_time = ((base_time - time.time()) % interval) or interval | |
time.sleep(next_time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment