本文整理汇总了Python中multiprocessing.Manager.shutdown方法的典型用法代码示例。如果您正苦于以下问题:Python Manager.shutdown方法的具体用法?Python Manager.shutdown怎么用?Python Manager.shutdown使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.Manager
的用法示例。
在下文中一共展示了Manager.shutdown方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __main
# 需要导入模块: from multiprocessing import Manager [as 别名]
# 或者: from multiprocessing.Manager import shutdown [as 别名]
def __main(args, options):
'''Main program - accepts an options struct.'''
# Parse and validate command-line arguments
in_file, info_file, segment_file, out_dir = args
options.out_dir = args[3] # Useful shortcut
try:
if options.num_processes > 1:
manager = Manager()
lock = manager.Lock()
else:
lock = None
start = time.time()
# Load SNP info
info = im.io.read_info_npz(info_file)
if options.debug >= 1:
_writeln('haps %d, snps %d, region size %d snps, processes %d' % \
(2 * info.num_samples, info.num_snps, options.region_size, options.num_processes), lock)
# Read list of regions to process from stdin/in_file. If empty, process all regions
regions = map(int, ([options.snp_index / options.region_size] if options.snp_index else
(options.regions if options.regions else
(sys.stdin if in_file == '-' else open(in_file, 'rb')).readlines())))
num_regions = (info.num_snps + options.region_size - 1) / options.region_size
if not regions:
regions = range(num_regions)
_writeln('regions ' + repr(regions) + ' num_regions ' + repr(num_regions) +
' segment threshold ' + repr(options.min_len) + ' Mbp algorithm ' + options.algorithm + ' margin ' + repr(options.margin), lock)
# Process each SNP region [start,stop) independently
if options.save:
util.mkdir_if_not_exists(out_dir)
# Save index metadata, if processing the first region
if options.save and (options.force_save_metadata or 0 in regions):
if options.debug >= 1:
_writeln('Writing metadata to %s/metadata' % (out_dir,), lock)
np.savez('%s/metadata' % (out_dir,), snp=info.snp, region_size=options.region_size)
process = _process_region_profile if options.debug >= 2 else _process_region
if options.num_processes > 1:
# Multi-process mode. Map phase:build and save regional index files
po = Pool(processes=options.num_processes)
po.map(process, ((info, segment_file, out_dir, options, i, lock) for i in (i for i in regions if i >= 0 and i < num_regions)))
else:
# Single-process mode.
for i in (i for i in regions if i >= 0 and i < num_regions):
process((info, segment_file, out_dir, options, i, None))
# Reduce phase - nothing to do here
t = time.time() - start
if options.debug >= 1:
_writeln('Elapsed Time: %.3f sec (%.3f sec/region)' % (t, t / len(regions)), lock)
if options.num_processes > 1:
manager.shutdown()
except:
traceback.print_exc(file=sys.stdout)
sys.exit(util.EXIT_FAILURE)
示例2: start_server
# 需要导入模块: from multiprocessing import Manager [as 别名]
# 或者: from multiprocessing.Manager import shutdown [as 别名]
def start_server(self):
#use a generic manager's shared dictionary to handle strings and ints
manager = Manager()
dictionary = manager.dict()
dictionary["key"] = ''
dictionary["finished chunks"] = 0
# update is an event intended to be set by server to let the UI know that the shared dictionary has been updated.
update = Event()
update.clear()
# shutdown is linked to the server/client shared shutdown command, setting this should shutdown server and clients.
shutdown = Event()
shutdown.clear()
# shared is just an array, each shared variable is added to allow passing all shared values using one variable.
shared = []
shared.append(dictionary)
shared.append(shutdown)
shared.append(update)
preset = 8
if self.settings[preset]["single"] == "False":
network = True
else:
network = False
self.server = Process(target=Server, args=(self.settings[preset], shared))
self.server.start()
print "server pid: %i" % self.server.pid
# this is a very simple example of how an update loop in the UI classes might work
while not shutdown.is_set():
# update is an Event, this means that a process can use the .wait() command to block until it is .set()
update.wait(timeout=.5)
print "%i/%i chunks completed." % (dictionary["finished chunks"], dictionary["total chunks"])
print "Current word: %s" % dictionary["current word"]
if preset > 8:
print "%i hashes found." % dictionary["hashes found"]
os.system('cls' if os.name == 'nt' else 'clear')
if dictionary["key"] is not '':
print "Printing key from start_server method: " + dictionary["key"]
break
#print "%d chunks completed of %i total." % (dictionary["finished chunks"], dictionary["total chunks"])
# after UI data has been updated, .clear() the update event so it will wait again in the next iteration
update.clear()
shutdown.set()
time.sleep(1)
#self.server.terminate()
self.server.join()
self.server.terminate()
#os.kill(self.server.pid, signal.SIGKILL)
self.server.join()
manager.shutdown()
if network:
print self.server.pid
示例3: multi_mode
# 需要导入模块: from multiprocessing import Manager [as 别名]
# 或者: from multiprocessing.Manager import shutdown [as 别名]
#.........这里部分代码省略.........
if multi_total < cli_parsed.threads:
num_threads = multi_total
else:
num_threads = cli_parsed.threads
for i in xrange(num_threads):
targets.put(None)
try:
workers = [Process(target=worker_thread, args=(
cli_parsed, targets, lock, (multi_counter, multi_total))) for i in xrange(num_threads)]
for w in workers:
w.start()
for w in workers:
w.join()
except Exception as e:
print str(e)
# Set up UA table here
if cli_parsed.cycle is not None:
ua_dict = get_ua_values(cli_parsed.cycle)
if not cli_parsed.ua_init:
dbm.clear_table("ua")
completed = dbm.get_complete_http()
completed[:] = [x for x in completed if x.error_state is None]
for item in completed:
for browser, ua in ua_dict.iteritems():
dbm.create_ua_object(item, browser, ua)
cli_parsed.ua_init = True
dbm.clear_table("opts")
dbm.save_options(cli_parsed)
for browser, ua in ua_dict.iteritems():
targets = m.Queue()
multi_counter.value = 0
multi_total = dbm.get_incomplete_ua(targets, browser)
if multi_total > 0:
print("[*] Starting requests for User Agent {0}"
" ({1} Hosts)").format(browser, str(multi_total))
if multi_total < cli_parsed.threads:
num_threads = multi_total
else:
num_threads = cli_parsed.threads
for i in xrange(num_threads):
targets.put(None)
workers = [Process(target=worker_thread,
args=(cli_parsed, targets, lock,
(multi_counter, multi_total),
(browser, ua)))
for i in xrange(num_threads)]
for w in workers:
w.start()
for w in workers:
w.join()
if any((cli_parsed.vnc, cli_parsed.rdp)):
log._LOG_LEVEL = log.Level.ERROR
multi_total, targets = dbm.get_incomplete_vnc_rdp()
if multi_total > 0:
print ''
print 'Starting VNC/RDP Requests ({0} Hosts)'.format(str(multi_total))
app = QtGui.QApplication(sys.argv)
timer = QTimer()
timer.start(10)
timer.timeout.connect(lambda: None)
# add qt4 reactor
import qt4reactor
qt4reactor.install()
from twisted.internet import reactor
for target in targets:
if os.path.dirname(cli_parsed.d) != os.path.dirname(target.screenshot_path):
target.set_paths(cli_parsed.d)
tdbm = db_manager.DB_Manager(cli_parsed.d + '/ew.db')
if target.proto == 'vnc':
reactor.connectTCP(
target.remote_system, target.port,
vnc_module.RFBScreenShotFactory(
target.screenshot_path, reactor, app,
target, tdbm))
else:
reactor.connectTCP(
target.remote_system, int(target.port),
rdp_module.RDPScreenShotFactory(
reactor, app, 1200, 800,
target.screenshot_path, cli_parsed.timeout,
target, tdbm))
reactor.runReturn()
app.exec_()
if display is not None:
display.stop()
results = dbm.get_complete_http()
vnc_rdp = dbm.get_complete_vnc_rdp()
dbm.close()
m.shutdown()
write_vnc_rdp_data(cli_parsed, vnc_rdp)
sort_data_and_write(cli_parsed, results)
示例4: ProcessManager
# 需要导入模块: from multiprocessing import Manager [as 别名]
# 或者: from multiprocessing.Manager import shutdown [as 别名]
#.........这里部分代码省略.........
tbl = self.cur.execute("SELECT Value from log where Id = ?;", (iid,) )
if iid_start>0:
self.cur.fetchmany(iid_start)
ret=[]
for pos in self.cur.fetchall():
ret.append(pos[0])
return ret
def pop_messages(self, id):
"""Get messages from thread and remove its from thread queue
Args:
id - thread id
"""
iid = int(id)
ret = self.get_messages(iid)
self.cur.execute("delete from where Id = ?;", (iid,) )
return ret
def kill_thread(self, id):
"""Kill thread
Args:
id - thread id
"""
iid = int(id)
if iid in self.process_list:
p = self.process_list[iid]
if p.status == 1:
p.input_queue.put("^C")
def remove_thread(self, id):
"""Kill thread and return thread messages
Args:
id - thread id
"""
iid = int(id)
self.kill_thread(iid)
self.get_messages(iid)
def list_threads(self, all=False):
"""Return threads list
Args:
all - if True, return all threads, if False, return only active threads
"""
ret = []
for key in self.process_list:
p = self.process_list[key]
if p.status in (0,1) or all:
ret.append(p)
return ret
def thread_info(self, id):
"""Return thread informations
Args:
id - thread id
"""
iid = int(id)
if iid in self.process_list:
return self.process_list[iid]
else:
return None
def kill_all(self):
"""Kill all threads"""
while not self.task_queue.empty():
self.task_queue.get()
threads = self.list_threads(all=True)
for thread in threads:
self.kill_thread(thread.id)
time.sleep(1)
for worker in self.workers:
worker[2].terminate()
if self.manager:
self.manager.shutdown()
self.manager = None
def wait_for_result(self):
"""wait for any message from threads"""
if self.manager:
self._wait_completion()
tasks = self.list_threads(all=True)
for task in tasks:
msgs=self.get_messages(task.id)
self._process_output_queue()
return None
def on_idle(self):
if self.manager:
self._process_output_queue()
示例5: Monitor
# 需要导入模块: from multiprocessing import Manager [as 别名]
# 或者: from multiprocessing.Manager import shutdown [as 别名]
class Monitor(object):
"""
Contains monitoring data and provides interface for manipulating it
from detached threads/processes
"""
def __init__(self, ctx, port):
self.ctx = ctx
self.port = port
self.manager = Manager()
self.queue = self.manager.Queue()
self.stats = StatsProxy(self.queue)
self.__shutdown_request = False
self.__stats = Stats('monitor')
if self.ctx.stat_format == STAT_TEXT:
self.stats_file = 'stats.txt'
elif self.ctx.stat_format == STAT_JSON:
self.stats_file = 'stats.json'
else:
self.stats_file = 'stats'
self.stats_file = os.path.join(ctx.tmp_dir, self.stats_file)
self.d_thread = Thread(target=self.data_thread, name="MonitorDataThread")
self.d_thread.daemon = True
if self.port:
if socket.has_ipv6:
HTTPServer.address_family = socket.AF_INET6
address = '::'
else:
HTTPServer.address_family = socket.AF_INET
address = '0.0.0.0'
server_address = (address, self.port)
self.httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
self.l_thread = Thread(target=self.listen_thread, name="MonitorListenThread")
self.l_thread.daemon = True
self.u_thread = Thread(target=self.update_thread, name="MonitorUpdateThread")
self.u_thread.daemon = True
self.d_thread.start()
if self.port:
self.l_thread.start()
self.u_thread.start()
def update(self):
"""
Writes to file current stats
"""
stats_file_tmp = os.path.join(self.stats_file + '.tmp')
with open(stats_file_tmp, 'w') as f:
if self.ctx.stat_format == STAT_TEXT:
f.write(str(self.__stats))
elif self.ctx.stat_format == STAT_JSON:
f.write(self.__stats.json())
f.write('\n')
try:
if os.path.exists(stats_file_tmp):
os.rename(stats_file_tmp, self.stats_file)
except Exception as e:
self.log.error("Failed to rename {0} to {1}: {2}".format(stats_file_tmp, self.stats_file, e))
def data_thread(self):
"""
TODO: Not very pythonish interface, but OK for now.
"""
import Queue
while not self.__shutdown_request:
try:
data = self.queue.get(block=True, timeout=1)
except Queue.Empty:
continue
except:
self.log.exception('Failed to wait on queue')
break
try:
prefix = data[0]
stats = self.__stats
# If prefix was set then use sub stat
if prefix:
for sub in prefix.split('\\'):
stats = stats[sub]
# Use different handling for different stat flavours
flavour = data[1]
if flavour == StatsProxy.COUNTER:
_, _, name, value = data
counter = getattr(stats.counter, name)
if value > 0:
counter += value
else:
counter -= -value
elif flavour == StatsProxy.SET_COUNTER:
_, _, name, value = data
counter = getattr(stats.counter, name)
if value > 0:
counter.success = value
elif value < 0:
counter.failures = -value
#.........这里部分代码省略.........
示例6: RemoteExperiment
# 需要导入模块: from multiprocessing import Manager [as 别名]
# 或者: from multiprocessing.Manager import shutdown [as 别名]
class RemoteExperiment(Experiment):
def __init__(self, socket, title='', measlist=[], stamp=None):
self.manager = Manager()
self.output = self.manager.dict()
self.s = socket
if stamp == None:
self.title = title
self.measlist = measlist
else:
self.title, self.measlist = self.get_experiment(stamp)
self.stamp = stamp
self.plots = []
self.figs = []
self._data = pd.DataFrame
def get_experiment(self, stamp):
return ask_socket(self.s, 'self.get_experiment(\'%s\')' %stamp)
def new_file(self):
return True
@property
def running(self):
running = self.datacollector.is_alive()
if not running:
if len(self.plots)>0:
if self.plots[0]['type'] is not 'pcolor':
display.clear_output(wait=True)
return running
def create_remote(self):
self.stamp = ask_socket(self.s, 'self.create_experiment(\'%s\', %s)' %(self.title, self.measlist))
def start_remote(self):
ask_socket(self.s, 'self.experiments[\'%s\'].run()' %self.stamp)
def set(self, *args, **kwargs):
'''
Add keyword argument as measurement type
to measurement list
'''
measlist = self.measlist.copy()
for key in kwargs:
measlist.append({'type':key, 'params': kwargs[key]})
self.measlist = measlist
def restart_datacollector(self):
self.manager = Manager()
self.output = self.manager.dict()
if hasattr(self, 'datacollector'):
if self.datacollector.is_alive():
self.datacollector.terminate()
self.datacollector = RemoteDataCollector(socket=self.s, title=self.title, output=self.output, stamp=self.stamp, save_data=False)
# self.output['data'] = self._data
self.datacollector.start()
def run(self):
if self.measlist is []:
raise NameError('Measurement is not defined.')
if 'measure' not in [meas['type'] for meas in self.measlist]:
print('Warning: No \'measure\' command found.')
self.create_remote()
if hasattr(self, 'datacollector'):
if self.datacollector.is_alive():
self.datacollector.terminate()
self.datacollector = RemoteDataCollector(socket=self.s, title=self.title, output=self.output, stamp=self.stamp, save_data=False)
self.datacollector.start()
self.start_remote()
def __del__(self):
self.manager.shutdown()
if hasattr(self, 'datacollector'):
if self.datacollector.is_alive():
self.datacollector.terminate()
self.datacollector.exitcode
示例7: __main
# 需要导入模块: from multiprocessing import Manager [as 别名]
# 或者: from multiprocessing.Manager import shutdown [as 别名]
def __main(args, options):
'''Main program - accepts an options struct.'''
# Parse and validate command-line arguments
in_file, info_file, segment_file, out_dir = args
options.out_dir = args[3] # Useful shortcut
try:
# Initialize thread pool
if options.num_processes > 1:
manager = Manager()
lock = manager.Lock()
else: lock = None
start = time.time()
# Load SNP info
info = im.io.read_info_npz(info_file)
if options.debug >= 1:
_writeln('haps %d, snps %d, region size %d snps, processes %d' % \
(2 * info.num_samples, info.num_snps, options.region_size, options.num_processes), lock)
# Read list of regions to process from stdin/in_file. If empty, process all regions.
# If a region index list is read, IT MUST BE CONTIGUOUS! (e.g. [3, 4, 5, 6])
regions = map(int, ([options.snp_index / options.region_size] if options.snp_index is not None else
(options.regions if options.regions else
(sys.stdin if in_file == '-' else open(in_file, 'rb')).readlines())))
num_regions = (info.num_snps + options.region_size - 1) / options.region_size
if not regions: regions = range(num_regions)
_writeln('regions ' + repr(regions) + ' num_regions ' + repr(num_regions) +
' segment threshold ' + repr(options.min_len) + ' Mbp algorithm ' + options.algorithm + ' margin ' + repr(options.margin), lock)
# Save index metadata if first region is processed in this run
if options.save: util.mkdir_if_not_exists(out_dir)
if options.save and (options.force_save_metadata or 0 in regions):
if options.debug >= 1: _writeln('Writing metadata to %s/metadata' % (out_dir,), lock)
np.savez('%s/metadata' % (out_dir,), snp=info.snp, region_size=options.region_size)
# segments = _SegmentCollection(info, segment_file, regions, options)
# print segments
# Map phase: process each SNP independently
r = segments.region_info
# snps = [(r['region'][0], options.snp_index - r['snp_start'][0])] if options.snp_index is not None else \
# ((region, snp) for region, start_raw, stop_raw in zip(r['region'], r['snp_start'], r['snp_stop'])
# for snp in xrange(start_raw, stop_raw))
snp_processor = _new_snp_processor(info, segments, options, lock)
if options.num_processes > 1:
# Multi-process mode. SNPs are processed in parallel.
po = Pool(processes=options.num_processes)
#a = MyObject()
#result = po.map(process_snp, ((a, region, snp) for region, snp in snps))
#result = po.map(process_snp, ((region, snp) for region, snp in snps))
#result = po.map(snp_processor.process, ((region, snp) for region, snp in snps))
result = po.map(snp_processor.process, ((region, snp) for region, snp in snps))
else:
# Single-process mode (sequential)
#result = [snp_processor.process((region, snp)) for region, snp in snps]
for region in regions:
result = [snp_processor.process(segment_file, region,((region, snp)) for region, snp in snps)]
print result
# Reduce phase: organize results in array and save to npz files
_save_index(segments.region_info, result, options.save, out_dir)
t = time.time() - start
if options.debug >= 1: _writeln('Elapsed Time: %.3f sec (%.3f sec/region)' % (t, t / len(regions)), lock)
if options.num_processes > 1: manager.shutdown()
except:
traceback.print_exc(file=sys.stdout)
sys.exit(util.EXIT_FAILURE)
示例8: __init__
# 需要导入模块: from multiprocessing import Manager [as 别名]
# 或者: from multiprocessing.Manager import shutdown [as 别名]
class Server:
def __init__(self, hostname="", port=0, num_workers=None):
self.hostname = hostname
self.port = port
self.num_workers = num_workers
self.manager = Manager()
self.status = self.manager.dict()
self.queue = self.manager.Queue()
self.waiting = self.manager.dict()
def handle_request(self, request):
try:
logger.debug("Received request %r", request)
return request.handle(self.queue, self.status)
except Exception:
logger.error("Invalid request %r", request, exc_info=True)
def handle_client(self, conn):
logger.debug("Accepted client connection.")
try:
while True:
request = conn.recv()
response = self.handle_request(request)
if response is not None:
conn.send(response)
except EOFError:
logger.debug("Client connection closed.")
def wait_for_clients(self, serv):
while True:
client = serv.accept()
self.handle_client(client)
def start(self):
"""Starts a server that controls local workers.
Calling this function starts a pool of `num_workers` workers used to run
targets sent to the server. The server will run indefinitely unless shut
down by the user.
"""
try:
serv = Listener((self.hostname, self.port))
workers = Pool(
processes=self.num_workers,
initializer=Worker,
initargs=(self.status, self.queue, self.waiting),
)
logging.info(
"Started %s workers, listening on port %s",
self.num_workers,
serv.address[1],
)
self.wait_for_clients(serv)
except OSError as e:
if e.errno == 48:
raise ServerError(
(
"Could not start workers listening on port {}. "
"The port may already be in use."
).format(self.port)
)
except KeyboardInterrupt:
logging.info("Shutting down...")
workers.close()
workers.join()
self.manager.shutdown()
示例9: Experiment
# 需要导入模块: from multiprocessing import Manager [as 别名]
# 或者: from multiprocessing.Manager import shutdown [as 别名]
#.........这里部分代码省略.........
if plot['type']=='plot':
x,y = plot['args'][0], plot['args'][1]
if type(y) == str:
y = [y]
for yname,line in zip(y,ax.lines):
tasks.append(asyncio.ensure_future(self.update_line(ax, line, x, yname)))
if plot['type']=='pcolor':
x,y,z = plot['x'], plot['y'], plot['z']
tasks.append(asyncio.ensure_future(self.update_pcolor(ax, x, y, z)))
loop.run_until_complete(asyncio.wait(tasks))
display.clear_output(wait=True)
display.display(*self.figs)
time.sleep(0.1)
except KeyboardInterrupt:
loop.run_until_complete(asyncio.wait(tasks))
display.clear_output(wait=True)
display.display(*self.figs)
self._user_interrupt = True
def pcolor(self, xname, yname, zname, *args, **kwargs):
title = self.wait_and_get_title()
x,y,z = self._data[xname], self._data[yname], self._data[zname]
shape = (len(y.unique()), len(x.unique()))
diff = shape[0]*shape[1] - len(z)
Z = np.concatenate((z.values, np.zeros(diff))).reshape(shape)
df = pd.DataFrame(Z, index=y.unique(), columns=x.unique())
ax = sns.heatmap(df)
pl.title(title)
pl.xlabel(xname)
pl.ylabel(yname)
ax.invert_yaxis()
pl.plt.show()
self.plots.append({'type': 'pcolor', 'x':xname, 'y':yname, 'z':zname, 'args':args, 'kwargs':kwargs, 'ax':ax})
if ax.get_figure() not in self.figs:
self.figs.append(ax.get_figure())
@asyncio.coroutine
def update_pcolor(self, ax, xname, yname, zname):
x,y,z = self._data[xname], self._data[yname], self._data[zname]
shape = (len(y.unique()), len(x.unique()))
diff = shape[0]*shape[1] - len(z)
Z = np.concatenate((z.values, np.zeros(diff))).reshape(shape)
df = pd.DataFrame(Z, index=y.unique(), columns=x.unique())
cbar_ax = ax.get_figure().axes[1]
sns.heatmap(df, ax=ax, cbar_ax=cbar_ax)
ax.set_xlabel(xname)
ax.set_ylabel(yname)
ax.invert_yaxis()
@asyncio.coroutine
def update_line(self, ax, hl, xname, yname):
del hl._xorig, hl._yorig
hl.set_xdata(self._data[xname])
hl.set_ydata(self._data[yname])
ax.relim()
ax.autoscale()
gc.collect()
def set(self, **kwargs):
self.measurement.set(**kwargs)
def sweep(self, sweep_param, arr=[]):
ins, param = re.split('\.', sweep_param)
return Sweep(self, ins, param, arr)
def do_while(self, clause):
self.set(do_while = clause)
def do(self, func, *args):
self.set(do = (func, args))
def measure(self, params=None):
self.set(measure = params)
def run(self):
if self.measurement.measlist is []:
raise NameError('Measurement is not defined.')
if 'measure' not in [meas['type'] for meas in self.measurement.measlist]:
print('Warning: No \'measure\' command found.')
if self.measurement.pid is not None:
measlist = self.measurement.measlist
self.measurement = Measurement(self.instruments,
measlist)
if self.datacollector.is_alive():
self.datacollector.terminate()
self.datacollector = DataCollector(self.measurement.pipe[1],
self.output,
self.title)
if not self.datacollector.is_alive():
self.datacollector.start()
if not self.measurement.is_alive():
self.measurement.start()
def __del__(self):
self.manager.shutdown()
if self.datacollector.is_alive():
self.datacollector.terminate()
self.datacollector.exitcode
self.measurement.exitcode
示例10: roll
# 需要导入模块: from multiprocessing import Manager [as 别名]
# 或者: from multiprocessing.Manager import shutdown [as 别名]
def roll(self, paths=None, rev=None, outgoing=None, workdir=None, num_procs=None):
"""Run all of the registered linters against the specified file paths.
:param paths: An iterable of files and/or directories to lint.
:param rev: Lint all files touched by the specified revision.
:param outgoing: Lint files touched by commits that are not on the remote repository.
:param workdir: Lint all files touched in the working directory.
:param num_procs: The number of processes to use. Default: cpu count
:return: A dictionary with file names as the key, and a list of
:class:`~result.ResultContainer`s as the value.
"""
paths = paths or []
if isinstance(paths, basestring):
paths = [paths]
if not self.linters:
raise LintersNotConfigured
# Calculate files from VCS
if rev:
paths.extend(self.vcs.by_rev(rev))
if workdir:
paths.extend(self.vcs.by_workdir())
if outgoing:
paths.extend(self.vcs.outgoing(outgoing))
paths = paths or ['.']
paths = map(os.path.abspath, paths)
# Set up multiprocessing
m = Manager()
queue = m.Queue()
for linter in self.linters:
queue.put(linter['path'])
num_procs = num_procs or cpu_count()
num_procs = min(num_procs, len(self.linters))
pool = Pool(num_procs)
all_results = defaultdict(list)
workers = []
for i in range(num_procs):
workers.append(
pool.apply_async(_run_worker, args=(queue, paths), kwds=self.lintargs))
pool.close()
# ignore SIGINT in parent so we can still get partial results
# from child processes. These should shutdown quickly anyway.
orig_sigint = signal.signal(signal.SIGINT, signal.SIG_IGN)
self.failed = []
for worker in workers:
# parent process blocks on worker.get()
results, failed = worker.get()
if failed:
self.failed.extend(failed)
for k, v in results.iteritems():
all_results[k].extend(v)
signal.signal(signal.SIGINT, orig_sigint)
m.shutdown()
return all_results