Created
August 4, 2015 02:11
-
-
Save housemeow/795a42e53d34629c5cf7 to your computer and use it in GitHub Desktop.
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
class Data: | |
__data_instance = None | |
@classmethod | |
def get_instance(cls): | |
if cls.__data_instance is None: | |
cls.__data_instance = Data() | |
return cls.__data_instance | |
def __init__(self): | |
self.data = 0 | |
self.loop = True |
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
from flask import Flask, jsonify | |
from multiprocessing import Process | |
from time import sleep | |
from data import Data | |
app = Flask(__name__) | |
def env_ctr_loop(): | |
data = Data().get_instance() | |
while True: | |
if data.loop: | |
data.data += 1 | |
sleep(3) | |
print data.data | |
p = Process(target=env_ctr_loop) | |
p.start() | |
@app.route("/") | |
def hello(): | |
data = Data().get_instance() | |
result = { | |
"data.data": data.data | |
} | |
return jsonify(**result) | |
@app.route("/start") | |
def start(): | |
data = Data().get_instance() | |
data.loop = True | |
return "Start success" | |
@app.route("/end") | |
def end(): | |
data = Data().get_instance() | |
data.loop = False | |
return "End success" | |
if __name__ == "__main__": | |
app.run(debug=True, host='0.0.0.0', threaded=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment