submitting cpu-bound tasks to processes and io-bound tasks to threads
Write a classic sequential program. Then convert it into a parallel one.
It runs faster.
Don’t use it.
for image in images: create_thumbnail(image)
from fork import fork for image in images: fork(create_thumbnail, image)
result = fork(my_func, *args, **kwargs)
It’s a proxy object that behaves almost exactly like the real return value of my_func except that it’s lazy.
Very lazy. You can even add, multiply, etc. such proxy results without blocking which come in quite handy, especially in loops. Use fork.await, str, print, etc. to force evaluation and get the real and non-lazy value back.
sizes = 0 for image in images: sizes += fork(create_thumbnail, image) # lazy evaluation print(sizes) # forces evaluation
You don’t need to bother. fork will take care of that for you.
You can assist fork by decorating your functions; not decorating defaults to fork.cpu_bound:
@io_bound def call_remote_webservice(): # implementation @cpu_bound def heavy_computation(n): # implementation
Original (sequential) tracebacks are preserved. That should make debugging easier. However, don’t try to catch exceptions. You better want to exit and see them. When you force evaluation potential exceptions will be raised.
If you really need more control over the type of execution, use fork.process or fork.thread. They work just like fork.fork but enforce the corresponding type of background execution.
import pkg_resources for worker_function in pkg_resources.iter_entry_points(group='worker'): process(worker_function)
You can shorten your programs by using fork.map. It works like fork.fork but submits a function multiple times for each item given by an iterable.
results = fork.map(create_thumbnail, images)
fork.map_process and fork.map_thread work accordingly and force a specific type of execution. Use those if really necessary. Otherwise, just use fork.map. fork take care for you in this case again.
In order to wait for the completion of a set of result proxies, use fork.await_all. If you want to unblock by the first unblocking result proxy, call fork.await_any.
There are also blocking variants available: fork.block_map, fork.block_map_process and fork.block_map_thread; in case you need some syntactic sugar:
fork.await_all(fork.map(create_thumbnail, images)) # equals fork.block_map(create_thumbnail, images)
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|File Name & Checksum SHA256 Checksum Help||Version||File Type||Upload Date|
|xfork-0.37-py2-none-any.whl (5.2 kB) Copy SHA256 Checksum SHA256||py2||Wheel||Feb 16, 2016|
|xfork-0.37-py3-none-any.whl (5.2 kB) Copy SHA256 Checksum SHA256||py3||Wheel||Feb 18, 2016|
|xfork-0.37.tar.gz (6.4 kB) Copy SHA256 Checksum SHA256||–||Source||Feb 16, 2016|