3 Commits
0.1.0 ... 0.1.3

Author SHA1 Message Date
ducoterra
e09bd887b5 fix imports 2020-11-29 18:52:23 -05:00
ducoterra
7358db6429 fix major variable issue 2020-11-29 16:36:27 -05:00
ducoterra
ec8a37af1c fix menu 2020-11-29 14:55:57 -05:00
2 changed files with 12 additions and 14 deletions

View File

@@ -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)
@@ -255,7 +253,7 @@ Let's get started:
19. Flip back over to your django terminal with the dropdown menu 19. Flip back over to your django terminal with the dropdown menu
20. Press ++ctrl+c++ to stop the server 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. 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.
@@ -346,8 +344,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 from concurrent.futures import ThreadPoolExecutor
import 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 +359,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 +378,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++
@@ -389,7 +386,6 @@ Let's make that a reality by simulating a slow internet connection.
```python ```python
import requests import requests
import json
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
def list_people(future, people): def list_people(future, people):
@@ -397,7 +393,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 +415,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 +426,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++

View File

@@ -32,3 +32,5 @@
### [Day 4](day4.md): while ### [Day 4](day4.md): while
- "While" - "While"
- Let's build a menu
- Threading