0

The following program is running. Not all the program is shown here. Only the relevant part is displayed here. The point is that I want to run the app at an regular interval to crawl new data from the web site. I have used thread and timer but failed.

import time, threading  
# some parts of the program are not shown here for their irrelevancy.
app = Flask(__name__)

@app.route('/')
@app.route('/weather')
def weather() -> 'html':
    place = "논현동 air quality information"
    # bringing in the crawled data 
    (match_web, match_db, update) = run_crawl()
    air_pm = str(match_web[0])
    ozone = str(match_web[1])
    total_air = str(match_web[2])
    update_time = update

    return render_template('weather.html', place = place, air_pm = air_pm, ozone 
= ozone, total_air = total_air, update_time = update)

if __name__ == '__main__':
    threading.Timer(100, app.run(debug=True, host='0.0.0.0')).start()

Here is the error message after the run

c:\yu_weather\flask-weather-yu>py -3 weather_crawling_rev7.py
* Restarting with stat
* Debugger is active!
* Debugger PIN: 224-483-614
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [23/May/2017 10:24:47] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [23/May/2017 10:24:47] "GET /favicon.ico HTTP/1.1" 404 -
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Python36-32\lib\threading.py", line 1182, in run
self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable

Exception ignored in: <module 'threading' from 'C:\\Python36-
32\\lib\\threading.py'>
Traceback (most recent call last):
File "C:\Python36-32\lib\threading.py", line 1294, in _shutdown
t.join()
File "C:\Python36-32\lib\threading.py", line 1056, in join
self._wait_for_tstate_lock()
File "C:\Python36-32\lib\threading.py", line 1072, in _wait_for_tstate_lock
elif lock.acquire(block, timeout):
KeyboardInterrupt
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Python36-32\lib\threading.py", line 1182, in run
self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable
Ryan R. Rosario
  • 5,114
  • 9
  • 41
  • 56
passion
  • 387
  • 4
  • 16

1 Answers1

0

The second argument of Timer must be a function. Try this

threading.Timer(100, lambda: app.run(debug=False, host='0.0.0.0')).start()
Andrei Cioara
  • 3,404
  • 5
  • 34
  • 62
  • Also note that the first argument is in seconds. I believe you wanted it to be 0.1 – Andrei Cioara May 23 '17 at 02:14
  • try your suggestion. but the error broke out. Any other suggestion, please? – passion May 23 '17 at 06:00
  • File "C:\Python36-32\lib\signal.py", line 47, in signal handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler)) ValueError: signal only works in main thread – passion May 23 '17 at 06:00
  • That happens because the debugging is set to True. Try this instead `threading.Timer(100, lambda: app.run(debug=False, host='0.0.0.0')).start()` Updated my answer accordingly Credits: https://stackoverflow.com/questions/31264826/start-a-flask-application-in-separate-thread – Andrei Cioara May 24 '17 at 02:02