--|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|--
k | e | y | w | o | r | d | a | b | c | f | g | h | i | j | l | m | n | p | q | s | t | u | v | x | z
-----
+---
# Duplicate and extend your `affine_break()` function
---
-# `map()`
+# map()
A common task is to apply a function to each item in a sequence, returning a sequence of the results.
-```python```
+```python
def double(x):
return x * 2
Define a function that takes a possible key (keyword and cipher type) and returns the key and its fitness.
+* (Also pass in the message and the fitness function)
+
Use `map()` and `max()` to find the best key
---
-# `print()`
+# Arity of print()
How many arguments does this take?
What does `Pool.starmap()` do?
+---
+
+```python
+from multiprocessing import Pool
+
+def keyword_break_mp(message, wordlist=keywords, fitness=Pletters, chunksize=500):
+ helper_args = [??? for word in wordlist] # One tuple for each possible key
+ with Pool() as pool:
+ breaks = pool.starmap(keyword_break_worker, helper_args, chunksize)
+ return max(breaks, key=lambda k: k[1])
+
+def keyword_break_worker(???):
+ ???
+ return (key, fitness)
+```
+
+* Gotcha: the function in `Pool.starmap()` must be defined at the top level
+ * This is definitely a "feature"
+
+---
+
+# Performance and chunksize
+
+Try the multiprocessing keyword break. Is it using all the resources?
+
+Setting `chunksize` is an art.
+
+## Map-reduce as a general pattern for multiprocessing
+
</textarea>
<script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">
</script>