本文整理汇总了Python中PyQt4.QtCore.QThreadPool.start方法的典型用法代码示例。如果您正苦于以下问题:Python QThreadPool.start方法的具体用法?Python QThreadPool.start怎么用?Python QThreadPool.start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt4.QtCore.QThreadPool
的用法示例。
在下文中一共展示了QThreadPool.start方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Tasks
# 需要导入模块: from PyQt4.QtCore import QThreadPool [as 别名]
# 或者: from PyQt4.QtCore.QThreadPool import start [as 别名]
class Tasks(QObject):
def __init__(self, ar, process_result):
super(Tasks, self).__init__()
self.ar = ar
self.process_result = process_result
self.pool = QThreadPool()
self.pool.setMaxThreadCount(10)
# def process_result(self, rev):
# # print 'Receiving', rev
# self.ref.setText(rev)
def start(self):
self.factory = GenerWork(self.ar)
self.factory.generateWorkers()
workers = self.factory.get_workers()
# print workers
for worker in workers:
worker.signals.result.connect(self.process_result)
self.pool.start(worker)
self.pool.waitForDone()
return data
def get_shell_data(self):
return self.factory.get_shell_data()
示例2: omap
# 需要导入模块: from PyQt4.QtCore import QThreadPool [as 别名]
# 或者: from PyQt4.QtCore.QThreadPool import start [as 别名]
def omap(fkt, par):
"""
:type fkt: ()->
:type par: list
:rtype: list
"""
# Die Anzahl der Prozesse ist ohne Aufwand auf einen idealen Wert beschränkt
pool = QThreadPool()
erg = [None] * len(par)
for p in par:
pool.start(QRunnable(fkt, p))
示例3: QWorker
# 需要导入模块: from PyQt4.QtCore import QThreadPool [as 别名]
# 或者: from PyQt4.QtCore.QThreadPool import start [as 别名]
class QWorker(QObject):
"""
Starts the QRunnable in a QThreadPool, inherits QObject because
we need to connect signals from the objects we are creating to
the functions that update the UI
"""
def __init__(self):
"""
This class creates new instances of QRunner, which is the class
that directly interfaces with the external executable
We need to create the QRunner/QWorker routine because a QRunnable
does not inherit from QObject and therefore does not have access
to the signal/slot mechanisms that we need in order to receive when
things get done.
QWorker inherits from QObject, so we need to directly call QObjects
constructor to initialize the underlying C++ object, super() does not
allow for this (seemingly) so we need to call it directly.
"""
# init QObject
QObject.__init__(self)
# Our own threadpool, for adding QRunners
self.threadpool = QThreadPool()
self.jobs = []
def create_job(self, ifname, ofname, cls):
"""
This method takes three arguments:
ifname is the input file name of the file to be converted
ofname is a string which is to be the output of the filename
cls is a reference to the calling class, so that we can connect
a signal to a slot.
"""
# Create the QRunner object and pass it filenames
runner = QRunner(ifname, ofname, cls)
# using our own connect method inherited from QObject
# connect the QRunner created before and use it's QObject
# to connect a signal to the slot
self.connect(runner.q_object, SIGNAL(
"finished()"), cls.update_progress_bar)
# ask our threadpool to run the task
self.threadpool.start(runner)
示例4: start
# 需要导入模块: from PyQt4.QtCore import QThreadPool [as 别名]
# 或者: from PyQt4.QtCore.QThreadPool import start [as 别名]
def start(self):
self.idmap = {}
self.entries = []
pool = QThreadPool()
pool.setMaxThreadCount(1)
for label in LABELS:
feed = Feed(label, self)
pool.start(feed)
imap = Imap(label, self)
pool.start(imap)
pool.waitForDone()
self.done.emit()
示例5: QFileWorker
# 需要导入模块: from PyQt4.QtCore import QThreadPool [as 别名]
# 或者: from PyQt4.QtCore.QThreadPool import start [as 别名]
class QFileWorker(object):
def __init__(self, deletions, cls):
"""
This class handles deletion of temporary file objects once processing
has completed.
It takes a reference to the class because we re-enable it's button
because we disabled it during processing
"""
self.deletions = deletions
self.threadpool = QThreadPool()
self.cls = cls
self._start()
def _start(self):
deleter = QFileCleaner(self.deletions, self.cls)
self.threadpool.start(deleter)
self.threadpool.waitForDone()
示例6: KISS_Thread
# 需要导入模块: from PyQt4.QtCore import QThreadPool [as 别名]
# 或者: from PyQt4.QtCore.QThreadPool import start [as 别名]
class KISS_Thread(QtCore.QThread):
"""
AX.25 Communication
"""
packet_received = pyqtSignal("QString", name="packetReceived")
def __init__(self, control_system):
"""Make a instance of the ReaderAndWriterThread class.
Args:
protocol (SerialProtocol): It is a instance of a communication protocol.
"""
QThread.__init__(self, control_system)
self.writer_thread = QThreadPool(self)
self.writer_thread.setMaxThreadCount(1)
def run(self):
print "kiss started"
self.kiss_connect()
def read_callback(self, data):
print "received packet, len", len(data)
kiss_data = kiss.constants.FEND + kiss.util.escape_special_codes(data) + kiss.constants.FEND
log_the_data("./log_files/telemetry_log_files/BSU_satellite.kss", kiss_data)
data = data[1:]
if len(data) < 15:
print "bad packet"
return
dest = "".join([chr(ord(c) >> 1) for c in data[:6]])
# print "Destination", dest
src = "".join([chr(ord(c) >> 1) for c in data[7:13]])
#print "Source", src
if not dest.startswith(LOCAL_SSID):
print "packet not for us"
return
start = 16
ssid = ord(data[13]) & 1
if ssid == 0:
via = "".join([chr(ord(c) >> 1) for c in data[7:13]])
start = 23
size = len(data) - start
if size == 0:
print "packet is empty"
return
payload = data[start:]
# self.control_system.on_packet_received(payload)
self.packet_received.emit(payload)
def kiss_connect(self):
try:
print "connect"
self.kiss_prot = kiss.KISS(port=config.kiss_serial_name, speed=config.kiss_baudrate)
self.kiss_prot.start()
self.kiss_prot.read(self.read_callback)
except:
error_mesage = "CANNOT OPEN PORT"
log_the_error(error_mesage)
sound.play(error_mesage)
print sys.exc_info()
print "ax.25 is failed"
def send_command(self, name, arg, device, time_of_execution):
data = {
"Timestamp": int(time.time()),
"Schedule": time_of_execution,
"Cmd": name,
"Arg": arg,
"Device": device
}
json_data = json.dumps(data)
print "Formed packet", json_data
writer = KissWriter(self.kiss_prot, LOCAL_SSID, REMOTE_SSID)
writer.set_data(json_data)
self.writer_thread.start(writer)
示例7: CatalogDialogTool
# 需要导入模块: from PyQt4.QtCore import QThreadPool [as 别名]
# 或者: from PyQt4.QtCore.QThreadPool import start [as 别名]
#.........这里部分代码省略.........
# validate filters
if not errors:
self.filters.validate(errors)
if errors:
self.iface.messageBar().pushMessage("Error", "The following errors occurred: " + "<br />".join(errors), level=QgsMessageBar.CRITICAL)
else:
self.init_layers()
self.dialog_ui.tab_widget.setCurrentIndex(RESULTS_TAB_INDEX)
next_x_list = self.drange_list(float(left) + INCREMENTAL_INTERVAL, float(right), INCREMENTAL_INTERVAL)
next_y_list = self.drange_list(float(bottom) + INCREMENTAL_INTERVAL, float(top), INCREMENTAL_INTERVAL)
self.init_progress_bar(len(next_x_list) * len(next_y_list))
self.model = CatalogTableModel(self.dialog_ui.table_view)
self.dialog_ui.table_view.setModel(self.model)
self.dialog_ui.table_view.selectionModel().selectionChanged.connect(self.selection_changed)
if not self.query:
self.query = GBDQuery(username=username, password=password, api_key=api_key)
filters = self.filters.get_query_filters()
time_begin = self.filters.get_datetime_begin()
time_end = self.filters.get_datetime_end()
current_x = float(left)
current_y = float(bottom)
for next_x in next_x_list:
for next_y in next_y_list:
search_runnable = CatalogSearchRunnable(self.query, self.model, self, top=next_y, left=current_x, right=next_x, bottom=current_y,
time_begin=time_begin, time_end=time_end, filters=filters)
search_runnable.task_object.task_complete.connect(self.on_search_complete)
self.search_thread_pool.start(search_runnable)
current_y = next_y
current_y = bottom
current_x = next_x
def reset(self):
self.filters.remove_all()
def export(self):
self.export_thread_pool.waitForDone(0)
acquisitions = None
if self.model is not None:
acquisitions = self.model.data
if not acquisitions:
self.iface.messageBar().pushMessage("Error", "No data to export.", level=QgsMessageBar.CRITICAL)
else:
# open file ui
select_file_ui = QFileDialog()
starting_file = self.export_file or os.path.expanduser("~")
export_file = select_file_ui.getSaveFileName(None, "Choose output file", starting_file, SELECT_FILTER)
if export_file:
self.export_file = export_file
self.init_progress_bar(0)
export_runnable = CatalogExportRunnable(acquisitions, self.export_file)
export_runnable.task_object.task_complete.connect(self.on_export_complete)
self.export_thread_pool.start(export_runnable)
@pyqtSlot()
def on_search_complete(self):
thread_count = self.get_search_active_thread_count()
if self.progress_message_bar:
示例8: GridZoneGeneratorDialog
# 需要导入模块: from PyQt4.QtCore import QThreadPool [as 别名]
# 或者: from PyQt4.QtCore.QThreadPool import start [as 别名]
#.........这里部分代码省略.........
def on_mirRadioButton_toggled(self, toggled):
if toggled:
self.mirLineEdit.setEnabled(True)
else:
self.mirLineEdit.setEnabled(False)
@pyqtSlot(bool)
def on_miRadioButton_toggled(self, toggled):
if toggled:
self.miLineEdit.setEnabled(True)
else:
self.miLineEdit.setEnabled(False)
@pyqtSlot(bool)
def on_indexRadioButton_toggled(self, toggled):
if toggled:
self.indexLineEdit.setEnabled(True)
else:
self.indexLineEdit.setEnabled(False)
@pyqtSlot(bool)
def on_saveButton_clicked(self):
fileName = QFileDialog.getSaveFileName(parent=self, caption=self.tr('Save Output File'), filter='Shapefile (*.shp)')
self.saveEdit.setText(fileName)
@pyqtSlot()
def on_button_box_accepted(self):
stopScale = int(self.stopScaleCombo.currentText().replace('k',''))
scale = int(self.scaleCombo.currentText().replace('k',''))
if stopScale > scale:
QMessageBox.warning(self, self.tr('Warning!'), self.tr('The stop scale denominator should not be bigger than \
the scale denominator!'))
return
if self.createShapeBox.isChecked() and not self.saveEdit.text():
QMessageBox.warning(self, self.tr('Warning!'), self.tr('A output file must be specified!'))
return
if not self.crsLineEdit.text():
QMessageBox.warning(self, self.tr('Warning!'), self.tr('A CRS must be specified!'))
return
if not self.validateMI():
QMessageBox.warning(self, self.tr('Warning!'), self.tr('Invalid Map Index!'))
return
# Initiating processing
gridThread = UtmGrid()
if self.createShapeBox.isChecked():
self.layer = None
else:
self.layer = gridThread.createGridLayer(self.tr('Grid Zones'), 'Multipolygon', self.crs.authid())
gridThread.setParameters(self.indexLineEdit.text(), stopScale, self.miLineEdit.text(), self.crs, self.saveEdit.text(), self.layer)
# Connecting end signal
gridThread.aux.processFinished.connect(self.finishProcess)
gridThread.aux.rangeCalculated.connect(self.setRange)
gridThread.aux.errorOccurred.connect(self.showError)
gridThread.aux.stepProcessed.connect(self.updateProgress)
# Setting the progress bar
self.progressMessageBar = self.iface.messageBar().createMessage(self.tr('Generating grid, please wait...'))
self.progressBar = QtGui.QProgressBar()
self.progressBar.setAlignment(Qt.AlignLeft|Qt.AlignVCenter)
self.progressMessageBar.layout().addWidget(self.progressBar)
self.iface.messageBar().pushWidget(self.progressMessageBar, self.iface.messageBar().INFO)
self.progressBar.setRange(0, 0)
self.progressMessageBar.destroyed.connect(gridThread.aux.cancel)
# Starting process
self.threadpool.start(gridThread)
def setRange(self, maximum):
self.progressBar.setRange(0, maximum)
def showError(self, msg):
QMessageBox.warning(self, self.tr('Warning!'), msg)
def updateProgress(self):
self.progressBar.setValue(self.progressBar.value() + 1)
def finishProcess(self):
self.progressBar.setValue(self.progressBar.maximum())
if self.createShapeBox.isChecked():
layer = self.iface.addVectorLayer(self.saveEdit.text(), self.tr('Grid Zones'), 'ogr')
self.updateMapCanvas(layer)
else:
QgsMapLayerRegistry.instance().addMapLayer(self.layer)
self.updateMapCanvas(self.layer)
QMessageBox.information(self, self.tr('Success!'), self.tr('Grid Created successfully!'))
def updateMapCanvas(self, layer):
if layer:
self.iface.setActiveLayer(layer)
layer.updateExtents()
bbox = self.iface.mapCanvas().mapSettings().layerToMapCoordinates(layer, layer.extent())
self.iface.mapCanvas().setExtent(bbox)
self.iface.mapCanvas().refresh()
示例9: __init__
# 需要导入模块: from PyQt4.QtCore import QThreadPool [as 别名]
# 或者: from PyQt4.QtCore.QThreadPool import start [as 别名]
class CSVGenerator:
def __init__(self, left, top, right, bottom, csv_filename, username, password, client_id, client_secret, days_to_query=60):
self.left = left
self.top = top
self.right = right
self.bottom = bottom
self.csv_filename = csv_filename
self.days_to_query = days_to_query
self.begin_date = None
self.end_date = None
self.username = username
self.password = password
self.client_id = client_id
self.client_secret = client_secret
# throw up a progress dialog
min_progress = 0.0
max_progress = ((self.right - self.left) / INCREMENTAL_INTERVAL) * \
((self.top - self.bottom) / INCREMENTAL_INTERVAL)
self.current_progress = min_progress
self.progress_dialog = QProgressDialog("Building up CSV file", "Abort", int(min_progress), int(max_progress),
None)
self.progress_dialog.setCancelButton(None)
self.progress_dialog.setWindowTitle("CSV Output")
self.progress_dialog.setLabelText("Building up CSV file")
self.progress_dialog.setMinimumDuration(0)
self.progress_dialog.setValue(0)
self.progress_dialog.show()
self.csv_elements = []
self.csv_generator_object = CSVGeneratorObject(self)
self.vector_header_dict = {}
self.pool = QThreadPool()
self.finished_submissions = False
self.lock = Lock()
def generate_csv(self):
# dates
now = datetime.now()
self.end_date = now
self.begin_date = now - timedelta(days=self.days_to_query)
current_x = self.left
current_y = self.bottom
serial_no = 1
# get header dict
vector_query = VectorQuery(username=self.username, password=self.password, client_id=self.client_id, client_secret=self.client_secret)
vector_query.log_in()
vector_params = InfoCubeVectorParams(top=self.top, bottom=self.bottom, left=self.left, right=self.right, time_begin=self.begin_date, time_end=self.end_date)
header_result = vector_query.get_vector_result(vector_params)
self.vector_header_dict = vector_query.get_vector_data(header_result)
for next_x in drange(self.left + INCREMENTAL_INTERVAL, self.right, INCREMENTAL_INTERVAL):
for next_y in drange(self.bottom + INCREMENTAL_INTERVAL, self.top, INCREMENTAL_INTERVAL):
username = self.username
password = self.password
client_id = self.client_id
client_secret = self.client_secret
csv_runnable = CSVRunnable(username, password, client_id, client_secret,
serial_no, next_y, current_x, next_x,
current_y, self.begin_date, self.end_date, self.vector_header_dict)
csv_runnable.csv_object.new_csv_element.connect(self.csv_generator_object.callback)
self.pool.start(csv_runnable)
serial_no += 1
current_y = next_y
current_y = self.bottom
current_x = next_x
self.finished_submissions = True
def on_completion(self):
self.csv_elements.sort(key=lambda element: element.serial_no)
log.info("Sort complete")
# write file
csv_file = open(self.csv_filename, 'w')
# write the header
header = CSVOutput.get_csv_header()
if self.vector_header_dict:
for term in self.vector_header_dict:
header = header + str(term) + ","
header = header[:-1]
csv_file.write(header)
csv_file.write("\n")
for csv_element in self.csv_elements:
#.........这里部分代码省略.........
示例10: WorkHandler
# 需要导入模块: from PyQt4.QtCore import QThreadPool [as 别名]
# 或者: from PyQt4.QtCore.QThreadPool import start [as 别名]
class WorkHandler(object):
"""
Class to handle threading of "work" (concretely this is just the evaluation of a function of the
form func(*args, **kwargs).
The process ID identifies (uniquely) each instance of a Worker; but the caller also defines
an ID which is then used to identify the Worker through the API.
"""
class WorkListener(with_metaclass(ABCMeta, object)):
"""
This abstract base class defines methods which must be overriden and which
handle responses to certain worker actions such as raised errors, or completion.
"""
def __init__(self):
pass
@abstractmethod
def on_processing_finished(self, result):
pass
@abstractmethod
def on_processing_error(self, error):
pass
def __init__(self):
self.thread_pool = None
self._listener = {}
self._worker = {}
self.thread_pool = QThreadPool()
def _add_listener(self, listener, process_id, id):
if not isinstance(listener, WorkHandler.WorkListener):
raise ValueError("The listener is not of type "
"WorkListener but rather {}".format(type(listener)))
self._listener.update({process_id: {'id': id, 'listener': listener}})
@pyqtSlot()
def on_finished(self, process_id):
if process_id not in self._worker:
return
result = self._worker.pop(process_id)['worker'].result
self._listener.pop(process_id)['listener'].on_processing_finished(result)
@pyqtSlot()
def on_error(self, process_id, error):
if process_id not in self._worker:
return
self._listener[process_id]['listener'].on_processing_error(error)
def process(self, caller, func, id, *args, **kwargs):
"""
Process a function call with arbitrary arguments on a new thread.
:param caller: ??
:param func: The function to be evaluated.
:param id: An identifying integer for the task.
:param args: args for func.
:param kwargs: keyword args for func.
"""
self.remove_already_processing(id)
process_id = uuid.uuid4()
# Add the caller
self._add_listener(caller, process_id, id)
finished_callback = functools.partial(self.on_finished, process_id)
error_callback = functools.partial(self.on_error, process_id)
worker = Worker(func, *args, **kwargs)
worker.signals.finished.connect(finished_callback)
worker.signals.error.connect(error_callback)
self._worker.update({process_id: {'id': id, 'worker': worker}})
self.thread_pool.start(self._worker[process_id]['worker'])
def remove_already_processing(self, id):
"""
Remove workers with ID
:param id:
"""
for key, process in list(self._listener.items()):
if process['id'] == id:
self._listener.pop(key)
self._worker.pop(key)
def wait_for_done(self):
self.thread_pool.waitForDone()
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __ne__(self, other):
return self.__dict__ != other.__dict__
示例11: MainWindow
# 需要导入模块: from PyQt4.QtCore import QThreadPool [as 别名]
# 或者: from PyQt4.QtCore.QThreadPool import start [as 别名]
#.........这里部分代码省略.........
def update_progress_bar(self):
"""
Method to update progress bar whilst running conversion
"""
# When we get a call it's from a thread finishing
# we increment the progress bar and check if we're at 100%
self.gui.progressBar.setValue(self.gui.progressBar.value()+1)
# This is bad, if a QProcess fails, we don't get a tick
# and the progressBar will never get to 100%
# we need to implement something that will catch errors with
# QProcess
if self.gui.progressBar.value() == self.gui.progressBar.maximum():
self.gui.progressBar.hide()
# create the deleter thread with a reference to the deletions
# list.
deleter = QFileWorker(self.deletions, self)
# wait for the deletions thread to return
deleter.threadpool.waitForDone()
# re-init deletions list so we don't try to delete
# already deleted files next time
self.deletions = []
# Re-enable the button
self.gui.btn_single_convert.setEnabled(True)
def convert(self):
"""
Implementation of multithreaded processing
"""
# if they click convert without looking for files
if len(self.gui.single_line_in.text()) < 1:
return
if len(self.gui.single_line_out.text()) < 1:
return
# Open the PDF that we found in the dialog
try:
pdf = PdfFileReader(open(self.gui.single_line_in.text(), 'rb'))
# if the file cannot be properly read raise error
except AssertionError:
QtGui.QMessageBox.warning(self, "Error", "This file is corrupt")
return
# if the file does not exist
except IOError:
QtGui.QMessageBox.warning(self, "Error", "Not a valid file path")
return
self.gui.btn_single_convert.setEnabled(False) # disable button
self.gui.progressBar.setValue(0) # re-init progress bar
# Show the progress bar
self.gui.progressBar.show()
# Set the progress bar's maximum number to the number of pages
# in the PDF
self.gui.progressBar.setMaximum(pdf.numPages)
# Send the PDF object (PdfFileReader) to the ThreadHandler
# along with a reference to ourself in order to set up signal
# callbacks
# most of the magic is in mthreading.py
# this is the cleanest interface I could come up with
# just pass it an opened PDF document and processing will begin
self.work.start(QThreadHandle(pdf, self))
def convert_dir(self):
"""
Method to convert a whole directory
"""
pass
def spawn_options(self):
"""
Spawns an options dialog
"""
options.OptionsDialog(self).exec_()
def spawn_about(self):
"""
Spawns an options dialog
"""
about_dialog = about.AboutDialog(self)
about_dialog.gui.label_2.setPixmap(QtGui.QPixmap(":/about.png"))
about_dialog.exec_()