Created
July 5, 2020 16:09
-
-
Save elbruno/2794f6b5995048701fe36122c982460c to your computer and use it in GitHub Desktop.
WebServerMultiThreadRequests.py
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
# Bruno Capuano | |
# start a webserver with flask in a thread | |
# start a different thread +1 a shared var | |
from flask import Flask | |
import threading | |
import time | |
iCounter = 0 | |
data = 'foo' | |
app = Flask(__name__) | |
def mainSum(): | |
# increment counter every second | |
global iCounter | |
while True: | |
iCounter = iCounter + 1 | |
t = time.localtime() | |
current_time = time.strftime("%H:%M:%S", t) | |
print(str(f"{current_time} - data {iCounter}")) | |
time.sleep(1) | |
def startWebServer(): | |
app.run(host='0.0.0.0', port=8080) | |
@app.route("/getdata") | |
def main(): | |
global iCounter | |
t = time.localtime() | |
current_time = time.strftime("%H:%M:%S", t) | |
return str(f"{current_time} - data {iCounter}") | |
if __name__ == "__main__": | |
stateThread = threading.Thread(target=mainSum) | |
stateThread.daemon = True | |
stateThread.start() | |
webThread = threading.Thread(target=startWebServer) | |
webThread.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment