Image('./talus-logo.png', height=300, width=300)
Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.
async/await
, this handles a lot of quick interactions that are staggeredos.fork()
is a classic exampleImage('gil.png', height=800, width=800)
os.fork()
import os, signal, time
pid = os.fork()
print(pid)
if pid == 0:
# Have the child do a thing
print('child starting', end='... ')
time.sleep(10)
print('child ending')
else:
# have the parent do a thing
os.kill(pid, signal.SIGINT)
60724 0 child starting... child ending
import multiprocessing
os.fork()
queues
and pipes
def worker(q, worknum):
print(f'Worker {worknum} Starting')
for i in range(2):
print(f'Worker {worknum} {q.get()}')
print(f'Worker {worknum} Exiting')
for i in range(3):
w = mp.Process(
name='example_worker',
target=worker,
args=(q, i)
).start()
for i in range(100): q.put(f'foo {i}', block=False)
Worker 0 Starting Worker 0 foo 0 Worker 1 Starting Worker 1 foo 1 Worker 0 foo 2 Worker 1 foo 3 Worker 2 Starting Worker 0 Exiting Worker 1 Exiting Worker 2 foo 4 Worker 2 foo 5 Worker 2 Exiting
find . -exec grep
is slowTL;DR without an IDE, finding arbitrary strings in a massive amount of text in a reasonably short amount of time.
!ba "Boulder Python" -d /Users/zoe/projects
/Users/zoe/projects/Boulder Python/Speed Snakes.ipynb:44 "* I'm a co-coordinator of the Boulder Python Meetup\n", /Users/zoe/projects/Boulder Python/.ipynb_checkpoints/Speed Snakes-checkpoint.ipynb:44 "* I'm a co-coordinator of the Boulder Python Meetup\n", /Users/zoe/projects/Boulder Python/Speed Snakes.slides.html:11921 <li>I'm a co-coordinator of the Boulder Python Meetup</li> --------------- Files Searched: 418 Files Matched: 3 Lines Searched: 59372 Duration: 0.1414327621459961
def main():
args = get_args()
processes, final_queue = ba.start_search(args)
print_process = processes[-1]
try:
print_process.join() # Wait main thread until printer is done
except (KeyboardInterrupt, EOFError): # kill all on ctrl+c/d
[p.terminate() for p in processes]
def index_worker(
directories: List[str], ignore_re: RETYPE, workers: int, search_queue: mp.Queue, output: mp.Queue, block=False
) -> None:
for dir in list(set(directories)): # no duplicate input
for subdir, _, files in os.walk(dir):
for question_file in files:
# we don't want to block, this process should be fastest
search_queue.put(
subdir + "/" + question_file, block=block, timeout=10
) # faster than os.path.join
for i in range(workers):
search_queue.put("EXIT") # poison pill workers
def file_searching_worker(
regex: RETYPE, ignore_re: RETYPE, filename_re: RETYPE, replace: Union[str, None],
search_queue: mp.Queue, output: mp.Queue,
) -> None:
# https://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python
rows, columns = os.popen("stty size", "r").read().split()
# Max width of printed line is 3/4 column count
maxwidth = int(3 * int(columns) / 4)
while True:
# we want to block this thread until we get search_queue
name = search_queue.get()
try:
with open(name, "r") as ofile:
flag = []
for line in ofile:
if regex.search(line):
found_string = line.split("\n")[0]
if len(found_string) > maxwidth:
found_string = found_string[:maxwidth] + "..."
flag.append((i, found_string))
if flag:
for value in flag:
output.put((name, value[0], value[1], regex))
except:
pass
def print_worker(
start_time: float, worker_count: int, output: mp.Queue,
final_queue: mp.Queue, pipemode: bool, editmode: bool,
) -> None:
while True:
statement = output.get()
if len(statement) == 4:
filename, linenum, matched, line = statement
replace = None
else:
filename, linenum, matched, line, replace = statement
final_queue.put(filename, linenum)
print(
"{}:{}\n\t{}".format(
filename,
color.fg256("#00ff00", linenum),
insert_colour(matched, line, extra_str=replace),
)
)
file_list.append((statement[1], statement[0]))
final_queue.put("EXIT")
EXIT
strings we pass around? How else do we stop a child?A programmer had a problem. He thought to himself, "I know, I'll solve it with threads!". has Now problems. two he
threading
- https://docs.python.org/3/library/threading.htmlos.fork()
- https://dataleek.io/index.php/2017/10/24/a-quick-guide-to-os-fork-in-python/multiprocessing
- https://docs.python.org/3.4/library/multiprocessing.html?highlight=process