Skip to content

Commit 35ce93a

Browse files
committed
First commit
0 parents  commit 35ce93a

12 files changed

+904
-0
lines changed

.gitignore

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
# File created using '.gitignore Generator' for Visual Studio Code: https://bit.ly/vscode-gig
2+
3+
# Created by https://www.toptal.com/developers/gitignore/api/visualstudiocode,macos,python
4+
# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudiocode,macos,python
5+
6+
### macOS ###
7+
# General
8+
.DS_Store
9+
.AppleDouble
10+
.LSOverride
11+
12+
# Icon must end with two \r
13+
Icon
14+
15+
# Thumbnails
16+
._*
17+
18+
# Files that might appear in the root of a volume
19+
.DocumentRevisions-V100
20+
.fseventsd
21+
.Spotlight-V100
22+
.TemporaryItems
23+
.Trashes
24+
.VolumeIcon.icns
25+
.com.apple.timemachine.donotpresent
26+
27+
# Directories potentially created on remote AFP share
28+
.AppleDB
29+
.AppleDesktop
30+
Network Trash Folder
31+
Temporary Items
32+
.apdisk
33+
34+
### Python ###
35+
# Byte-compiled / optimized / DLL files
36+
__pycache__/
37+
*.py[cod]
38+
*$py.class
39+
40+
# C extensions
41+
*.so
42+
43+
# Distribution / packaging
44+
.Python
45+
build/
46+
develop-eggs/
47+
dist/
48+
downloads/
49+
eggs/
50+
.eggs/
51+
lib/
52+
lib64/
53+
parts/
54+
sdist/
55+
var/
56+
wheels/
57+
share/python-wheels/
58+
*.egg-info/
59+
.installed.cfg
60+
*.egg
61+
MANIFEST
62+
63+
# PyInstaller
64+
# Usually these files are written by a python script from a template
65+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
66+
*.manifest
67+
*.spec
68+
69+
# Installer logs
70+
pip-log.txt
71+
pip-delete-this-directory.txt
72+
73+
# Unit test / coverage reports
74+
htmlcov/
75+
.tox/
76+
.nox/
77+
.coverage
78+
.coverage.*
79+
.cache
80+
nosetests.xml
81+
coverage.xml
82+
*.cover
83+
*.py,cover
84+
.hypothesis/
85+
.pytest_cache/
86+
cover/
87+
88+
# Translations
89+
*.mo
90+
*.pot
91+
92+
# Django stuff:
93+
*.log
94+
local_settings.py
95+
db.sqlite3
96+
db.sqlite3-journal
97+
98+
# Flask stuff:
99+
instance/
100+
.webassets-cache
101+
102+
# Scrapy stuff:
103+
.scrapy
104+
105+
# Sphinx documentation
106+
docs/_build/
107+
108+
# PyBuilder
109+
.pybuilder/
110+
target/
111+
112+
# Jupyter Notebook
113+
.ipynb_checkpoints
114+
115+
# IPython
116+
profile_default/
117+
ipython_config.py
118+
119+
# pyenv
120+
# For a library or package, you might want to ignore these files since the code is
121+
# intended to run in multiple environments; otherwise, check them in:
122+
# .python-version
123+
124+
# pipenv
125+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
126+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
127+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
128+
# install all needed dependencies.
129+
#Pipfile.lock
130+
131+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
132+
__pypackages__/
133+
134+
# Celery stuff
135+
celerybeat-schedule
136+
celerybeat.pid
137+
138+
# SageMath parsed files
139+
*.sage.py
140+
141+
# Environments
142+
.env
143+
.venv
144+
env/
145+
venv/
146+
ENV/
147+
env.bak/
148+
venv.bak/
149+
150+
# Spyder project settings
151+
.spyderproject
152+
.spyproject
153+
154+
# Rope project settings
155+
.ropeproject
156+
157+
# mkdocs documentation
158+
/site
159+
160+
# mypy
161+
.mypy_cache/
162+
.dmypy.json
163+
dmypy.json
164+
165+
# Pyre type checker
166+
.pyre/
167+
168+
# pytype static type analyzer
169+
.pytype/
170+
171+
# Cython debug symbols
172+
cython_debug/
173+
174+
### VisualStudioCode ###
175+
.vscode/*
176+
!.vscode/settings.json
177+
!.vscode/tasks.json
178+
!.vscode/launch.json
179+
!.vscode/extensions.json
180+
*.code-workspace
181+
182+
# Local History for Visual Studio Code
183+
.history/
184+
185+
### VisualStudioCode Patch ###
186+
# Ignore all local history of files
187+
.history
188+
.ionide
189+
190+
# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,macos,python
191+
192+
# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)
193+

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.9.5

example_1.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""This program demonstrates a synchronous approach
2+
to accomplishing tasks. The worker can't delegate
3+
any tasks and completes them all one after another.
4+
"""
5+
from time import sleep
6+
from queue import Queue
7+
from codetiming import Timer
8+
9+
10+
def task(delay: float=0):
11+
"""This is a little task that takes some time to complete
12+
13+
Args:
14+
delay (int): The delay the task takes
15+
"""
16+
with Timer(text="Task elapsed time: {:.2f} seconds"):
17+
sleep(delay)
18+
return delay
19+
20+
21+
def worker(name: str, task_queue: Queue):
22+
"""This is our worker that pulls tasks from
23+
the queue and performs them
24+
25+
Args:
26+
name (str): The string name of the task
27+
work_queue (Queue): The queue the tasks are pulled from
28+
"""
29+
# pull tasks from the queue until the queue is empty
30+
print(f"Worker {name} starting to run tasks")
31+
while not task_queue.empty():
32+
fn, kwargs = task_queue.get()
33+
result = fn(**kwargs)
34+
print(f"Worker {name} completed task: result = {result}\n")
35+
36+
print(f"Worker {name} finished as there are no more tasks\n")
37+
38+
39+
def main():
40+
"""
41+
This is the main entry point for the program
42+
"""
43+
# Create the queue for tasks
44+
task_queue = Queue()
45+
46+
# Put some tasks in the queue
47+
list(map(task_queue.put, [
48+
(task, {"delay": 4.0}),
49+
(task, {"delay": 3.0}),
50+
(task, {"delay": 2.0}),
51+
(task, {"delay": 1.0}),
52+
]))
53+
54+
# Create a single worker
55+
workers = [
56+
(worker, "One", task_queue)
57+
]
58+
59+
# Run the workers
60+
with Timer(text="Total elapsed time: {:.2f}"):
61+
while len(workers):
62+
for worker_ in workers:
63+
worker_fn, name, queue = worker_
64+
worker_fn(name, queue)
65+
workers.remove(worker_)
66+
67+
68+
if __name__ == "__main__":
69+
print()
70+
main()
71+
print()

example_2.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""This program demonstrates a synchronous approach
2+
to accomplishing tasks. The worker can't delegate
3+
any tasks and completes them all one after another.
4+
In this version there is a second worker created,
5+
but the two workers aren't cooperating with each
6+
other.
7+
"""
8+
from time import sleep
9+
from queue import Queue
10+
from codetiming import Timer
11+
12+
13+
def task(delay: float=0):
14+
"""This is a little task that takes some time to complete
15+
16+
Args:
17+
delay (int): The delay the task takes
18+
"""
19+
with Timer(text="Task elapsed time: {:.2f} seconds"):
20+
sleep(delay)
21+
return delay
22+
23+
24+
def worker(name: str, task_queue: Queue):
25+
"""This is our worker that pulls tasks from
26+
the queue and performs them
27+
28+
Args:
29+
name (str): The string name of the task
30+
work_queue (Queue): The queue the tasks are pulled from
31+
"""
32+
# pull tasks from the queue until the queue is empty
33+
print(f"Worker {name} starting to run tasks")
34+
while not task_queue.empty():
35+
fn, kwargs = task_queue.get()
36+
result = fn(**kwargs)
37+
print(f"Worker {name} completed task: result = {result}\n")
38+
39+
print(f"Worker {name} finished as there are no more tasks\n")
40+
41+
42+
def main():
43+
"""
44+
This is the main entry point for the program
45+
"""
46+
# Create the queue for tasks
47+
task_queue = Queue()
48+
49+
# Put some tasks in the queue
50+
list(map(task_queue.put, [
51+
(task, {"delay": 4.0}),
52+
(task, {"delay": 3.0}),
53+
(task, {"delay": 2.0}),
54+
(task, {"delay": 1.0}),
55+
]))
56+
57+
# Create two workers
58+
workers = [
59+
(worker, "One", task_queue),
60+
(worker, "Two", task_queue)
61+
]
62+
63+
# Run the workers
64+
with Timer(text="Total elapsed time: {:.2f}"):
65+
while len(workers):
66+
for worker_ in workers:
67+
worker_fn, name, queue = worker_
68+
worker_fn(name, queue)
69+
workers.remove(worker_)
70+
71+
72+
if __name__ == "__main__":
73+
print()
74+
main()
75+
print()

0 commit comments

Comments
 (0)