-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_stream.py
More file actions
38 lines (31 loc) · 1.22 KB
/
test_stream.py
File metadata and controls
38 lines (31 loc) · 1.22 KB
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
import datetime
import logging
import requests
STREAM_NUMBER = 1 # TODO: change this for second run
logger = logging.getLogger(__name__)
logging.basicConfig(**dict(
format='%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s',
level=logging.DEBUG,
handlers=[
logging.FileHandler(filename=f"logs/stream{STREAM_NUMBER}.log", encoding='utf-8'),
logging.StreamHandler(),
],
))
logger.info('Started downloading content')
# NOTE: Uncomment to check how SYNC view works
# r = requests.get('http://localhost:8000/sse', stream=True)
# NOTE: To check how ASYNC view works
r = requests.get('http://localhost:8000/sse_async', stream=True)
r.raise_for_status()
local_filename = f'logs/test_stream_data{STREAM_NUMBER}.txt'
logger.info('Opened file for writing')
with open(local_filename, 'wb') as f:
logger.info('Iterating over content chunks')
for chunk in r.iter_content(chunk_size=24):
# If you have chunk encoded response uncomment if
# and set chunk_size parameter to None.
# if chunk:
logger.info('Got chunk, writing to file')
f.write((str(datetime.datetime.now()) + ' - ' + chunk.decode() + '\n').encode())
logger.info('Wrote chunk to file')
logger.info('Done')