FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
My-Python-Code/Async-run-func.py at main · aminzayer/My-Python-Code · GitHub
aminzayer
/
My-Python-Code
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
My-Python-Code
/
Async-run-func.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
99 lines (83 loc) · 3.01 KB
Breadcrumbs
My-Python-Code
/
Async-run-func.py
Copy path
File metadata and controls
99 lines (83 loc) · 3.01 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import
time
import
asyncio
import
requests
import
aiohttp
from
asgiref
import
sync
def
timed
(
func
):
"""
records approximate durations of function calls
"""
def
wrapper
(
*
args
,
**
kwargs
):
start
=
time
.
time
()
print
(
'{name:<30} started'
.
format
(
name
=
func
.
__name__
))
result
=
func
(
*
args
,
**
kwargs
)
duration
=
"{name:<30} finished in {elapsed:.2f} seconds"
.
format
(
name
=
func
.
__name__
,
elapsed
=
time
.
time
()
-
start
)
print
(
duration
)
timed
.
durations
.
append
(
duration
)
return
result
return
wrapper
timed
.
durations
=
[]
@
timed
def
sync_requests_get_all
(
urls
):
"""
performs synchronous get requests
"""
# use session to reduce network overhead
session
=
requests
.
Session
()
return
[
session
.
get
(
url
).
json
()
for
url
in
urls
]
@
timed
def
async_requests_get_all
(
urls
):
"""
asynchronous wrapper around synchronous requests
"""
session
=
requests
.
Session
()
# wrap requests.get into an async function
def
get
(
url
):
return
session
.
get
(
url
).
json
()
async_get
=
sync
.
sync_to_async
(
get
)
async
def
get_all
(
urls
):
return
await
asyncio
.
gather
(
*
[
async_get
(
url
)
for
url
in
urls
])
# call get_all as a sync function to be used in a sync context
return
sync
.
async_to_sync
(
get_all
)(
urls
)
@
timed
def
async_aiohttp_get_all
(
urls
):
"""
performs asynchronous get requests
"""
async
def
get_all
(
urls
):
async
with
aiohttp
.
ClientSession
()
as
session
:
async
def
fetch
(
url
):
async
with
session
.
get
(
url
)
as
response
:
return
await
response
.
json
()
return
await
asyncio
.
gather
(
*
[
fetch
(
url
)
for
url
in
urls
])
# call get_all as a sync function to be used in a sync context
return
sync
.
async_to_sync
(
get_all
)(
urls
)
if
__name__
==
'__main__'
:
# this endpoint takes ~3 seconds to respond,
# so a purely synchronous implementation should take
# little more than 30 seconds and a purely asynchronous
# implementation should take little more than 3 seconds.
urls
=
[
'https://pubmed.ncbi.nlm.nih.gov/31456179/'
,
'https://pubmed.ncbi.nlm.nih.gov/20521754/'
,
'https://pubmed.ncbi.nlm.nih.gov/29284222/'
,
'https://pubmed.ncbi.nlm.nih.gov/15894099/'
,
'https://pubmed.ncbi.nlm.nih.gov/28298516/'
,
'https://pubmed.ncbi.nlm.nih.gov/24283956/'
,
'https://pubmed.ncbi.nlm.nih.gov/30005774/'
,
'https://pubmed.ncbi.nlm.nih.gov/28260181/'
,
'https://pubmed.ncbi.nlm.nih.gov/26580154/'
,
'https://pubmed.ncbi.nlm.nih.gov/28862198/'
,
'https://pubmed.ncbi.nlm.nih.gov/26059925/'
,
'https://pubmed.ncbi.nlm.nih.gov/28395765/'
,
'https://pubmed.ncbi.nlm.nih.gov/21969133/'
, ]
*
10
async_aiohttp_get_all
(
urls
)
async_requests_get_all
(
urls
)
sync_requests_get_all
(
urls
)
print
(
'----------------------'
)
[
print
(
duration
)
for
duration
in
timed
.
durations
]
Back
|
FazBrowse Home
|
New Git URL