Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nflgame.live run with Threading #80

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 50 additions & 10 deletions nflgame/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import requests
import logging
import os
import threading

try:
import pytz
Expand Down Expand Up @@ -95,6 +96,37 @@
"""


class LiveRunner(threading.Thread):
"""
Class to run checking for live games that are currently playing in a
separate thread that can be shutdown gracefully. Create a LiveRunner
instance with the same parameters as you would call run(), and
start running with LiveRunner.run(). You can stop running at any time with
LiveRunner.shutdown(). The run method just calls the nflgame.live run method
passing it's own thread's stop_event.
"""

def __init__(self, callback, active_interval=15, inactive_interval=900,
stop=None, *args, **kwargs):
threading.Thread.__init__(self, *args, **kwargs)
self.stop_event = threading.Event()
self.callback = callback
self.active_interval = active_interval
self.inactive_interval = inactive_interval
self.stop = stop

def run(self):
run(callback=self.callback,
active_interval=self.active_interval,
inactive_interval=self.inactive_interval,
stop_date=self.stop,
stop_event=self.stop_event)

def shutdown(self):
self.stop_event.set()
self.join()


def current_season_phase():
"""
Returns the current season phase
Expand Down Expand Up @@ -157,7 +189,8 @@ def current_games(year=None, week=None, kind=_cur_season_phase):
return current


def run(callback, active_interval=15, inactive_interval=900, wakeup_time=900, stop=None):
def run(callback, active_interval=15, inactive_interval=900, wakeup_time=900,
stop_date=None, stop_event=None):
"""
Starts checking for games that are currently playing.

Expand Down Expand Up @@ -189,15 +222,25 @@ def run(callback, active_interval=15, inactive_interval=900, wakeup_time=900, st
have started or are about to start. wakeup_time is used to add games to
the active list.

With the default parameters, run will never stop. However, you may set
stop to a Python datetime.datetime value. After time passes the stopping
With the default parameters, run will never stop. However, you may provide
a threading.Event object as the stop_event, or pass a datetime.datetime
value as the stop_date parameter. After time passes the stopping
point, run will quit. (Technically, it's possible that it won't quit until
at most inactive_interval seconds after the stopping point is reached.)
The stop value is compared against datetime.datetime.now().

If you provide a threading.Event as the stop_event, call it's set() method
to terminate the run. You can also use this module's LiveRunner class
to run this method in a separate thread making use of it's shutdown()
method.
"""
active = False
last_week_check = _update_week_number()

# If no stop_event given, we create this dummy stop_event that we never set.
if stop_event is None:
stop_event = threading.Event()

logger.info("Starting live loop")

# Before we start with the main loop, we make a first pass at what we
Expand All @@ -212,7 +255,8 @@ def run(callback, active_interval=15, inactive_interval=900, wakeup_time=900, st
_completed.append(info['eid'])

while True:
if stop is not None and datetime.datetime.now() > stop:
if stop_date is not None and datetime.datetime.now() > stop_date or \
stop_event.is_set():
return

if time.time() - last_week_check > _WEEK_INTERVAL:
Expand All @@ -222,14 +266,10 @@ def run(callback, active_interval=15, inactive_interval=900, wakeup_time=900, st

if active:
active = _run_active(callback, games)
if not active:
continue
time.sleep(active_interval)
else:
active = not _run_inactive(games)
if active:
continue
time.sleep(inactive_interval)

stop_event.wait(active_interval if active else inactive_interval)


def _run_active(callback, games):
Expand Down