Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7358db6429 | ||
|
|
ec8a37af1c |
20
docs/day4.md
20
docs/day4.md
@@ -129,14 +129,12 @@ Let's get started:
|
||||
|
||||
```python
|
||||
import requests
|
||||
import json
|
||||
```
|
||||
|
||||
3. Create the list_people function by adding the following:
|
||||
|
||||
```python hl_lines="5-7"
|
||||
```python hl_lines="3-5"
|
||||
import requests
|
||||
import json
|
||||
|
||||
def list_people(people):
|
||||
print(people)
|
||||
@@ -255,7 +253,7 @@ Let's get started:
|
||||
19. Flip back over to your django terminal with the dropdown menu
|
||||
20. Press ++ctrl+c++ to stop the server
|
||||
|
||||
### Threading
|
||||
## Threading
|
||||
|
||||
One of the more difficult concepts in programming is threading and multiprocessing. It's rarely taught at an intro level but it's fairly easy to use.
|
||||
|
||||
@@ -347,7 +345,7 @@ Let's make that a reality by simulating a slow internet connection.
|
||||
```python hl_lines="3 3"
|
||||
import requests
|
||||
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.
|
||||
@@ -362,7 +360,7 @@ Let's make that a reality by simulating a slow internet connection.
|
||||
while True:
|
||||
choice = input(f"Please choose an option [{', '.join(choices)}]: ")
|
||||
if choice == "list":
|
||||
people = list_people(future, people)
|
||||
future, people = list_people(future, people)
|
||||
elif choice == "update":
|
||||
future = ThreadPoolExecutor().submit(update_people, people)
|
||||
elif choice == "clear":
|
||||
@@ -381,7 +379,7 @@ Let's make that a reality by simulating a slow internet connection.
|
||||
people = future.result()
|
||||
future = None
|
||||
print(people)
|
||||
return people
|
||||
return (future, people)
|
||||
```
|
||||
|
||||
22. Save with ++ctrl+s++
|
||||
@@ -397,7 +395,7 @@ Let's make that a reality by simulating a slow internet connection.
|
||||
people = future.result()
|
||||
future = None
|
||||
print(people)
|
||||
return people
|
||||
return (future, people)
|
||||
|
||||
def update_people(people):
|
||||
try:
|
||||
@@ -419,7 +417,7 @@ Let's make that a reality by simulating a slow internet connection.
|
||||
while True:
|
||||
choice = input(f"Please choose an option [{', '.join(choices)}]: ")
|
||||
if choice == "list":
|
||||
people = list_people(future, people)
|
||||
future, people = list_people(future, people)
|
||||
elif choice == "update":
|
||||
future = ThreadPoolExecutor().submit(update_people, people)
|
||||
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.")
|
||||
```
|
||||
|
||||
24. Run
|
||||
25. Now run `python menu.py`
|
||||
24. 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.
|
||||
27. In a moment you'll see "successfully updated people" print. Type `list` and press ++enter++.
|
||||
28. Type `clear` and press ++enter++
|
||||
|
||||
@@ -31,4 +31,6 @@
|
||||
|
||||
### [Day 4](day4.md): while
|
||||
|
||||
- "While"
|
||||
- "While"
|
||||
- Let's build a menu
|
||||
- Threading
|
||||
Reference in New Issue
Block a user