-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tunining setting of notebook for timeout issue
- Loading branch information
1 parent
91ed4ac
commit e1ef376
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
UDACITY DL Nanodegree/Introduction to Neural Network/workspace_utils.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import signal | ||
|
||
from contextlib import contextmanager | ||
|
||
import requests | ||
|
||
|
||
DELAY = INTERVAL = 4 * 60 # interval time in seconds | ||
MIN_DELAY = MIN_INTERVAL = 2 * 60 | ||
KEEPALIVE_URL = "https://nebula.udacity.com/api/v1/remote/keep-alive" | ||
TOKEN_URL = "http://metadata.google.internal/computeMetadata/v1/instance/attributes/keep_alive_token" | ||
TOKEN_HEADERS = {"Metadata-Flavor":"Google"} | ||
|
||
|
||
def _request_handler(headers): | ||
def _handler(signum, frame): | ||
requests.request("POST", KEEPALIVE_URL, headers=headers) | ||
return _handler | ||
|
||
|
||
@contextmanager | ||
def active_session(delay=DELAY, interval=INTERVAL): | ||
""" | ||
Example: | ||
from workspace_utils import active session | ||
with active_session(): | ||
# do long-running work here | ||
""" | ||
token = requests.request("GET", TOKEN_URL, headers=TOKEN_HEADERS).text | ||
headers = {'Authorization': "STAR " + token} | ||
delay = max(delay, MIN_DELAY) | ||
interval = max(interval, MIN_INTERVAL) | ||
original_handler = signal.getsignal(signal.SIGALRM) | ||
try: | ||
signal.signal(signal.SIGALRM, _request_handler(headers)) | ||
signal.setitimer(signal.ITIMER_REAL, delay, interval) | ||
yield | ||
finally: | ||
signal.signal(signal.SIGALRM, original_handler) | ||
signal.setitimer(signal.ITIMER_REAL, 0) | ||
|
||
|
||
def keep_awake(iterable, delay=DELAY, interval=INTERVAL): | ||
""" | ||
Example: | ||
from workspace_utils import keep_awake | ||
for i in keep_awake(range(5)): | ||
# do iteration with lots of work here | ||
""" | ||
with active_session(delay, interval): yield from iterable |