本文整理匯總了Python中multiprocessing.pool方法的典型用法代碼示例。如果您正苦於以下問題:Python multiprocessing.pool方法的具體用法?Python multiprocessing.pool怎麽用?Python multiprocessing.pool使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類multiprocessing
的用法示例。
在下文中一共展示了multiprocessing.pool方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import pool [as 別名]
def __init__(self, batch_size, input_length, nthreads=6, web_viz=False):
super(RLDataIter, self).__init__()
self.batch_size = batch_size
self.input_length = input_length
self.env = [self.make_env() for _ in range(batch_size)]
self.act_dim = self.env[0].action_space.n
self.state_ = None
self.reset()
self.provide_data = [mx.io.DataDesc('data', self.state_.shape, np.uint8)]
self.web_viz = web_viz
if web_viz:
self.queue = queue.Queue()
self.thread = Thread(target=make_web, args=(self.queue,))
self.thread.daemon = True
self.thread.start()
self.nthreads = nthreads
if nthreads > 1:
self.pool = multiprocessing.pool.ThreadPool(6)
示例2: __init__
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import pool [as 別名]
def __init__(self, raster):
self._raster = raster
self._back_ds = raster.back_ds
self._alive = True
io_pool = raster.io_pool
if io_pool is not None:
if isinstance(io_pool, mp.pool.ThreadPool):
self._same_address_space = True
elif isinstance(io_pool, mp.pool.Pool):
self._same_address_space = False
else: # pragma: no cover
assert False, 'Type should be checked in facade'
self._waiting_room_address = '/Pool{}/WaitingRoom'.format(id(io_pool))
self._working_room_address = '/Pool{}/WorkingRoom'.format(id(io_pool))
self._waiting_jobs = set()
self._working_jobs = set()
self.address = '/Raster{}/FileChecker'.format(self._raster.uid)
示例3: test_imap_handle_iterable_exception
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import pool [as 別名]
def test_imap_handle_iterable_exception(self):
if self.TYPE == 'manager':
self.skipTest('test not appropriate for {}'.format(self.TYPE))
it = self.pool.imap(sqr, exception_throwing_generator(10, 3), 1)
for i in range(3):
self.assertEqual(next(it), i*i)
self.assertRaises(SayWhenError, it.next)
# SayWhenError seen at start of problematic chunk's results
it = self.pool.imap(sqr, exception_throwing_generator(20, 7), 2)
for i in range(6):
self.assertEqual(next(it), i*i)
self.assertRaises(SayWhenError, it.next)
it = self.pool.imap(sqr, exception_throwing_generator(20, 7), 4)
for i in range(4):
self.assertEqual(next(it), i*i)
self.assertRaises(SayWhenError, it.next)
示例4: test_imap_unordered_handle_iterable_exception
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import pool [as 別名]
def test_imap_unordered_handle_iterable_exception(self):
if self.TYPE == 'manager':
self.skipTest('test not appropriate for {}'.format(self.TYPE))
it = self.pool.imap_unordered(sqr,
exception_throwing_generator(10, 3),
1)
expected_values = map(sqr, range(10))
with self.assertRaises(SayWhenError):
# imap_unordered makes it difficult to anticipate the SayWhenError
for i in range(10):
value = next(it)
self.assertIn(value, expected_values)
expected_values.remove(value)
it = self.pool.imap_unordered(sqr,
exception_throwing_generator(20, 7),
2)
expected_values = map(sqr, range(20))
with self.assertRaises(SayWhenError):
for i in range(20):
value = next(it)
self.assertIn(value, expected_values)
expected_values.remove(value)
示例5: test_pool_worker_lifetime_early_close
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import pool [as 別名]
def test_pool_worker_lifetime_early_close(self):
# Issue #10332: closing a pool whose workers have limited lifetimes
# before all the tasks completed would make join() hang.
p = multiprocessing.Pool(3, maxtasksperchild=1)
results = []
for i in range(6):
results.append(p.apply_async(sqr, (i, 0.3)))
p.close()
p.join()
# check the results
for (j, res) in enumerate(results):
self.assertEqual(res.get(), sqr(j))
#
# Test that manager has expected number of shared objects left
#
示例6: LaunchDaskDistributedClient
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import pool [as 別名]
def LaunchDaskDistributedClient(self, scheduler_ip=None, scheduler_port=None):
if self.parallel and self.parallel_model == "dask" and self.is_dask_scheduler_initialised is False:
from multiprocessing.pool import ThreadPool
try:
import dask
from dask.distributed import Client, LocalCluster
except ImportError:
raise ImportError("dask is not installed. Install it 'using pip install dask[complete]'")
dask.config.set(pool=ThreadPool(self.no_of_cpu_cores))
# INITIALISE CLUSTER
if scheduler_ip is None:
cluster = LocalCluster(n_workers=self.no_of_cpu_cores, processes=False, threads_per_worker=None)
client = Client(cluster)
else:
client = Client(scheduler_ip)
self.dask_client = client
self.is_dask_scheduler_initialised = True
示例7: test_imap_handle_iterable_exception
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import pool [as 別名]
def test_imap_handle_iterable_exception(self):
if self.TYPE == 'manager':
self.skipTest('test not appropriate for {}'.format(self.TYPE))
it = self.pool.imap(sqr, exception_throwing_generator(10, 3), 1)
for i in range(3):
self.assertEqual(next(it), i*i)
self.assertRaises(SayWhenError, it.__next__)
# SayWhenError seen at start of problematic chunk's results
it = self.pool.imap(sqr, exception_throwing_generator(20, 7), 2)
for i in range(6):
self.assertEqual(next(it), i*i)
self.assertRaises(SayWhenError, it.__next__)
it = self.pool.imap(sqr, exception_throwing_generator(20, 7), 4)
for i in range(4):
self.assertEqual(next(it), i*i)
self.assertRaises(SayWhenError, it.__next__)
示例8: test_imap_unordered_handle_iterable_exception
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import pool [as 別名]
def test_imap_unordered_handle_iterable_exception(self):
if self.TYPE == 'manager':
self.skipTest('test not appropriate for {}'.format(self.TYPE))
it = self.pool.imap_unordered(sqr,
exception_throwing_generator(10, 3),
1)
expected_values = list(map(sqr, list(range(10))))
with self.assertRaises(SayWhenError):
# imap_unordered makes it difficult to anticipate the SayWhenError
for i in range(10):
value = next(it)
self.assertIn(value, expected_values)
expected_values.remove(value)
it = self.pool.imap_unordered(sqr,
exception_throwing_generator(20, 7),
2)
expected_values = list(map(sqr, list(range(20))))
with self.assertRaises(SayWhenError):
for i in range(20):
value = next(it)
self.assertIn(value, expected_values)
expected_values.remove(value)
示例9: test_unpickleable_result
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import pool [as 別名]
def test_unpickleable_result(self):
from multiprocessing.pool import MaybeEncodingError
p = multiprocessing.Pool(2)
# Make sure we don't lose pool processes because of encoding errors.
for iteration in range(20):
scratchpad = [None]
def errback(exc):
scratchpad[0] = exc
res = p.apply_async(unpickleable_result, error_callback=errback)
self.assertRaises(MaybeEncodingError, res.get)
wrapped = scratchpad[0]
self.assertTrue(wrapped)
self.assertIsInstance(scratchpad[0], MaybeEncodingError)
self.assertIsNotNone(wrapped.exc)
self.assertIsNotNone(wrapped.value)
p.close()
p.join()
示例10: test_pool_worker_lifetime_early_close
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import pool [as 別名]
def test_pool_worker_lifetime_early_close(self):
# Issue #10332: closing a pool whose workers have limited lifetimes
# before all the tasks completed would make join() hang.
p = multiprocessing.Pool(3, maxtasksperchild=1)
results = []
for i in range(6):
results.append(p.apply_async(sqr, (i, 0.3)))
p.close()
p.join()
# check the results
for (j, res) in enumerate(results):
self.assertEqual(res.get(), sqr(j))
#
# Test of creating a customized manager class
#
示例11: act
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import pool [as 別名]
def act(self, action):
if self.nthreads > 1:
new = self.pool.map(env_step, zip(self.env, action))
else:
new = [env.step(act) for env, act in zip(self.env, action)]
reward = np.asarray([i[1] for i in new], dtype=np.float32)
done = np.asarray([i[2] for i in new], dtype=np.float32)
channels = self.state_.shape[1]//self.input_length
state = np.zeros_like(self.state_)
state[:,:-channels,:,:] = self.state_[:,channels:,:,:]
for i, (ob, env) in enumerate(zip(new, self.env)):
if ob[2]:
state[i,-channels:,:,:] = env.reset().transpose((2,0,1))
else:
state[i,-channels:,:,:] = ob[0].transpose((2,0,1))
self.state_ = state
if self.web_viz:
try:
while self.queue.qsize() > 10:
self.queue.get(False)
except queue.Empty:
pass
frame = self.visual()
self.queue.put(frame)
return reward, done
示例12: tear_down
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import pool [as 別名]
def tear_down(self):
"""
Tear down the pool
"""
if self._stopped:
log.logger.info("ProcessPool has already stopped.")
return
self._stopped = True
self._pool.close()
self._pool.join()
log.logger.info("ProcessPool stopped.")
示例13: test_apply
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import pool [as 別名]
def test_apply(self):
papply = self.pool.apply
self.assertEqual(papply(sqr, (5,)), sqr(5))
self.assertEqual(papply(sqr, (), {'x':3}), sqr(x=3))
示例14: test_map
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import pool [as 別名]
def test_map(self):
pmap = self.pool.map
self.assertEqual(pmap(sqr, range(10)), map(sqr, range(10)))
self.assertEqual(pmap(sqr, range(100), chunksize=20),
map(sqr, range(100)))
示例15: test_map_unplicklable
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import pool [as 別名]
def test_map_unplicklable(self):
# Issue #19425 -- failure to pickle should not cause a hang
if self.TYPE == 'threads':
self.skipTest('test not appropriate for {}'.format(self.TYPE))
class A(object):
def __reduce__(self):
raise RuntimeError('cannot pickle')
with self.assertRaises(RuntimeError):
self.pool.map(sqr, [A()]*10)