Created
March 11, 2015 03:00
-
-
Save sudodo/4db5f1461705b77c1367 to your computer and use it in GitHub Desktop.
Python 2.x (not) list comprehension with multithreading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import multiprocessing | |
| import itertools | |
| def get_trajectory(arg1, arg1): | |
| return [999,888,777,666] | |
| def get_traj_as_star(arg): | |
| return get_trajectory(arg[1], arg[2]) | |
| def run_multi_particle(num_particle, num_time, num_mesh, multi_thread=True): | |
| if not multi_thread: | |
| return [get_trajectory(num_time, num_mesh) for _ in range(num_particle)] | |
| # get [get_trajectory(num_time, num_mesh) for _ in range(num_particle)] with multithreading way! | |
| cpus = 7 | |
| pool = multiprocessing.Pool(processes=cpus) | |
| ret = pool.map(get_traj_as_star, itertools.izip(range(num_particle), itertools.repeat(num_time), itertools.repeat(num_mesh))) | |
| pool.terminate() # mandatory | |
| return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment