Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7358db6429 |
18
docs/day4.md
18
docs/day4.md
@@ -129,14 +129,12 @@ Let's get started:
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
import requests
|
import requests
|
||||||
import json
|
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Create the list_people function by adding the following:
|
3. Create the list_people function by adding the following:
|
||||||
|
|
||||||
```python hl_lines="5-7"
|
```python hl_lines="3-5"
|
||||||
import requests
|
import requests
|
||||||
import json
|
|
||||||
|
|
||||||
def list_people(people):
|
def list_people(people):
|
||||||
print(people)
|
print(people)
|
||||||
@@ -347,7 +345,7 @@ Let's make that a reality by simulating a slow internet connection.
|
|||||||
```python hl_lines="3 3"
|
```python hl_lines="3 3"
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
import concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
```
|
```
|
||||||
|
|
||||||
20. We need to modify our main program to call the update_people function in a different thread. To do this we need to keep track of a "future" object.
|
20. We need to modify our main program to call the update_people function in a different thread. To do this we need to keep track of a "future" object.
|
||||||
@@ -362,7 +360,7 @@ Let's make that a reality by simulating a slow internet connection.
|
|||||||
while True:
|
while True:
|
||||||
choice = input(f"Please choose an option [{', '.join(choices)}]: ")
|
choice = input(f"Please choose an option [{', '.join(choices)}]: ")
|
||||||
if choice == "list":
|
if choice == "list":
|
||||||
people = list_people(future, people)
|
future, people = list_people(future, people)
|
||||||
elif choice == "update":
|
elif choice == "update":
|
||||||
future = ThreadPoolExecutor().submit(update_people, people)
|
future = ThreadPoolExecutor().submit(update_people, people)
|
||||||
elif choice == "clear":
|
elif choice == "clear":
|
||||||
@@ -381,7 +379,7 @@ Let's make that a reality by simulating a slow internet connection.
|
|||||||
people = future.result()
|
people = future.result()
|
||||||
future = None
|
future = None
|
||||||
print(people)
|
print(people)
|
||||||
return people
|
return (future, people)
|
||||||
```
|
```
|
||||||
|
|
||||||
22. Save with ++ctrl+s++
|
22. Save with ++ctrl+s++
|
||||||
@@ -397,7 +395,7 @@ Let's make that a reality by simulating a slow internet connection.
|
|||||||
people = future.result()
|
people = future.result()
|
||||||
future = None
|
future = None
|
||||||
print(people)
|
print(people)
|
||||||
return people
|
return (future, people)
|
||||||
|
|
||||||
def update_people(people):
|
def update_people(people):
|
||||||
try:
|
try:
|
||||||
@@ -419,7 +417,7 @@ Let's make that a reality by simulating a slow internet connection.
|
|||||||
while True:
|
while True:
|
||||||
choice = input(f"Please choose an option [{', '.join(choices)}]: ")
|
choice = input(f"Please choose an option [{', '.join(choices)}]: ")
|
||||||
if choice == "list":
|
if choice == "list":
|
||||||
people = list_people(future, people)
|
future, people = list_people(future, people)
|
||||||
elif choice == "update":
|
elif choice == "update":
|
||||||
future = ThreadPoolExecutor().submit(update_people, people)
|
future = ThreadPoolExecutor().submit(update_people, people)
|
||||||
elif choice == "clear":
|
elif choice == "clear":
|
||||||
@@ -430,8 +428,8 @@ Let's make that a reality by simulating a slow internet connection.
|
|||||||
print("Invalid choice. Please try again.")
|
print("Invalid choice. Please try again.")
|
||||||
```
|
```
|
||||||
|
|
||||||
24. Run
|
24. Now run `python menu.py`
|
||||||
25. Now run `python menu.py`
|
25. Type `list` and press ++enter++. Notice how there's nothing in the list.
|
||||||
26. Type `update` and press ++enter++. Notice the menu returns instantly.
|
26. Type `update` and press ++enter++. Notice the menu returns instantly.
|
||||||
27. In a moment you'll see "successfully updated people" print. Type `list` and press ++enter++.
|
27. In a moment you'll see "successfully updated people" print. Type `list` and press ++enter++.
|
||||||
28. Type `clear` and press ++enter++
|
28. Type `clear` and press ++enter++
|
||||||
|
|||||||
Reference in New Issue
Block a user