-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests_futures_graceful.py
More file actions
70 lines (62 loc) · 2.06 KB
/
requests_futures_graceful.py
File metadata and controls
70 lines (62 loc) · 2.06 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'''
Does requests_futures interrupt the sockets on exit?
Conclusion: even socket is graceful!
'''
from __future__ import annotations
from typing import *
from time import sleep
from threading import Thread
import concurrent.futures as futures
from requests import Response
from requests_futures.sessions import FuturesSession
def main():
with FuturesSession(max_workers=1) as session:
class T(Thread):
def run(self):
while True:
print('starting req...')
f: futures.Future[Response] = session.get(
# 'https://www.google.com',
'http://localhost',
headers = {
'Host': 'www.google.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; rv:68.0) Gecko/20100101 Firefox/68.0',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://www.google.com/',
'Origin': 'https://www.google.com',
'Connection': 'keep-alive',
'TE': 'Trailers',
},
)
futures.wait([f])
response = f.result()
print(response.text[:64])
sleep(1)
T().start
input('...')
print('session closed.')
def whatAboutSockets():
import socket
server = socket.socket()
server.bind(('localhost', 2333))
server.listen(1)
a = socket.socket()
a.connect(('localhost', 2333))
b, _ = server.accept()
def f():
try:
while True:
print('a recv', a.recv(999))
finally:
print('recver finally')
Thread(target=f).start()
input('...0')
b.send(b'asd')
input('...1')
a.close()
print('closed')
input('...2')
# main()
whatAboutSockets()