You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/lecture_10/lab.md
+87-1Lines changed: 87 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -253,8 +253,94 @@ df[1:50,:]
253
253
### Multithreaded file processing
254
254
255
255
## Task switching
256
+
There is a way how to run "multiple" things at once, which does not necessarily involve either threads or processes. In Julia this concept is called task switching or asynchronous programming, where we fire off our requests in a short time and let the cpu/os/network handle the distribution. As an example which we will try today is querrying a web API, which has some variable latency. In the usuall sequantial fashion we can always post querries one at a time, however generally the APIs can handle multiple request at a time, therefore in order to better utilize them, we can call them asynchronously and fetch all results later, in some cases this will be faster.
256
257
257
-
### Only if we I come up with something interesting
258
+
!!! info "Burst requests"
259
+
It is a good practice to check if an API supports some sort of batch request, because making a burst of single request might lead to a worse performance for others and a possible blocking of your IP/API key.
260
+
261
+
Consider following functions
262
+
```julia
263
+
functiona()
264
+
for i in1:10
265
+
sleep(1)
266
+
end
267
+
end
268
+
269
+
functionb()
270
+
for i in1:10
271
+
@asyncsleep(1)
272
+
end
273
+
end
274
+
275
+
functionc()
276
+
@syncfor i in1:10
277
+
@asyncsleep(1)
278
+
end
279
+
end
280
+
```
281
+
282
+
```@raw html
283
+
</div></div>
284
+
<details class = "solution-body">
285
+
<summary class = "solution-header">How much time will take the execution of each of them?:</summary><p>
Choose one of the free web APIs and query its endpoint using the `HTTP.jl` library. Implement both sequential and asynchronous version. Compare them on an burst of 10 requests.
303
+
304
+
**HINTS**:
305
+
- use `HTTP.request` for `GET` requests on your chosen API, e.g. `r = HTTP.request("GET", "https://catfact.ninja/fact")` for random cat fact
306
+
- converting body of a response can be done simply by constructing a `String` out of it - `String(r.body)`
307
+
- in order to parse a json string use `JSON.jl`'s parse function
308
+
- Julia offers `asyncmap` - asynchronous `map`
309
+
310
+
```@raw html
311
+
</div></div>
312
+
<details class = "solution-body">
313
+
<summary class = "solution-header">Solution:</summary><p>
314
+
```
315
+
316
+
```julia
317
+
using HTTP, JSON
318
+
319
+
functionquery_cat_fact()
320
+
r = HTTP.request("GET", "https://catfact.ninja/fact")
0 commit comments