mirror of
https://github.com/Brandon-Rozek/website.git
synced 2024-11-25 01:26:30 -05:00
Added post
This commit is contained in:
parent
08b80260d1
commit
a5a1ba29cd
1 changed files with 39 additions and 0 deletions
39
content/blog/pythonsetinterval.md
Normal file
39
content/blog/pythonsetinterval.md
Normal file
|
@ -0,0 +1,39 @@
|
|||
---
|
||||
title: "Python: Set Interval"
|
||||
date: 2020-02-25T21:34:03-05:00
|
||||
draft: false
|
||||
tags: [ "python" ]
|
||||
---
|
||||
|
||||
Javascript has a function called `setInterval` which given a length of time $T$ and a callback function, it will perform that function every $T$ milliseconds. For example, to print "Hello, World!" every 5 seconds:
|
||||
|
||||
```javascript
|
||||
setInterval(function() {
|
||||
console.log("Hello, World!")
|
||||
}, 5 * 1000)
|
||||
```
|
||||
|
||||
Wouldn't it be nice if Python had a similar functionality? Well thanks to [right2clicky](https://stackoverflow.com/a/48741004), there's a nice and quick way to implement one.
|
||||
|
||||
```python
|
||||
from threading import Timer
|
||||
|
||||
class Repeat(Timer):
|
||||
def run(self):
|
||||
while not self.finished.wait(self.interval):
|
||||
self.function(*self.args, **self.kwargs)
|
||||
```
|
||||
|
||||
Since `self.finished.wait` only returns `True` when the Event `self.finished` is set to true, the thread will keep waiting and calling the function for the set interval time period.
|
||||
|
||||
The same post has a usage example:
|
||||
|
||||
```python
|
||||
from time import sleep
|
||||
|
||||
t = Repeat(1.0, lambda: print("Hello, World!"))
|
||||
t.start()
|
||||
sleep(5)
|
||||
t.cancel()
|
||||
```
|
||||
|
Loading…
Reference in a new issue