FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Generate-TV-Script-Using-RNN/workspace_utils.py at master · pranaysingh25/Generate-TV-Script-Using-RNN · GitHub
pranaysingh25
/
Generate-TV-Script-Using-RNN
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Generate-TV-Script-Using-RNN
/
workspace_utils.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
54 lines (40 loc) · 1.5 KB
Breadcrumbs
Generate-TV-Script-Using-RNN
/
workspace_utils.py
Copy path
File metadata and controls
54 lines (40 loc) · 1.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
Back
|
FazBrowse Home
|
New Git URL