本文整理汇总了Python中PyQt4.QtCore.QMutex.tryLock方法的典型用法代码示例。如果您正苦于以下问题:Python QMutex.tryLock方法的具体用法?Python QMutex.tryLock怎么用?Python QMutex.tryLock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt4.QtCore.QMutex
的用法示例。
在下文中一共展示了QMutex.tryLock方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ImporterThread
# 需要导入模块: from PyQt4.QtCore import QMutex [as 别名]
# 或者: from PyQt4.QtCore.QMutex import tryLock [as 别名]
class ImporterThread(QThread):
def __init__(self, framework, parent = None):
QThread.__init__(self, parent)
self.framework = framework
self.qlock = QMutex()
self.cursor = None
QObject.connect(self, SIGNAL('quit()'), self.quitHandler)
QObject.connect(self, SIGNAL('started()'), self.startedHandler)
def run(self):
QObject.connect(self, SIGNAL('do_runImport()'), self.handle_runImport, Qt.DirectConnection)
self.exec_()
def quitHandler(self):
if self.cursor:
self.cursor.close()
self.exit(0)
def startedHandler(self):
pass
def runImport(self, importers, proxy_file, source, callback):
self.importers = importers
self.proxy_filelist = [proxy_file]
self.source = source
self.callbackObj = callback
QTimer.singleShot(50, self, SIGNAL('do_runImport()'))
def runImportList(self, importers, proxy_filelist, source, callback):
self.importers = importers
self.proxy_filelist = proxy_filelist
self.source = source
self.callbackObj = callback
QTimer.singleShot(50, self, SIGNAL('do_runImport()'))
def handle_runImport(self):
if self.qlock.tryLock():
try:
for proxy_file in self.proxy_filelist:
try:
self.framework.debug_log('attempting import of %s' % (str(proxy_file)))
self.importers.process_import(str(proxy_file), self.framework, self.source)
except Exception as ex:
self.framework.report_exception(ex)
finally:
self.qlock.unlock()
self.callbackObj.emit(SIGNAL('runImportFinished()'))
示例2: SiteMapThread
# 需要导入模块: from PyQt4.QtCore import QMutex [as 别名]
# 或者: from PyQt4.QtCore.QMutex import tryLock [as 别名]
class SiteMapThread(QThread):
def __init__(self, framework, treeViewModel, parent=None):
QThread.__init__(self, parent)
self.framework = framework
self.treeViewModel = treeViewModel
self.qtimer = QTimer()
self.qlock = QMutex()
self.fillAll = False
QObject.connect(self, SIGNAL("quit()"), self.quitHandler)
QObject.connect(self, SIGNAL("started()"), self.startedHandler)
self.re_set_cookie = re.compile(r"^Set-Cookie2?:\s*(.+)$", re.I | re.M)
self.re_host_name = re.compile(r"^Host:\s*(.+?)$", re.I | re.M)
self.lastId = 0
self.Data = None
self.cursor = None
def db_attach(self):
self.Data = self.framework.getDB()
self.cursor = self.Data.allocate_thread_cursor()
self.populateSiteMap(True)
def db_detach(self):
self.close_cursor()
self.Data = None
def close_cursor(self):
if self.cursor and self.Data:
self.cursor.close()
self.Data.release_thread_cursor(self.cursor)
self.cursor = None
def run(self):
QObject.connect(self, SIGNAL("updateSiteMap()"), self.doUpdateSiteMap, Qt.DirectConnection)
self.framework.subscribe_response_data_added(self.doUpdateSiteMap)
self.exec_()
def quitHandler(self):
self.framework.debug_log("SiteMapThread quit...")
self.close_cursor()
self.exit(0)
def startedHandler(self):
self.framework.debug_log("SiteMapThread started...")
self.framework.subscribe_database_events(self.db_attach, self.db_detach)
def populateSiteMap(self, fillAll):
self.fillAll = fillAll
QTimer.singleShot(50, self, SIGNAL("updateSiteMap()"))
def doUpdateSiteMap(self):
if not self.qlock.tryLock():
return
try:
if self.fillAll:
self.fillAll = False
self.treeViewModel.clearModel()
self.lastId = 0
rows = self.Data.get_sitemap_info(self.cursor, self.lastId)
global_cookie_jar = self.framework.get_global_cookie_jar()
count = 0
for row in rows:
count += 1
if 0 == (count % 100):
self.yieldCurrentThread()
rowItems = [m or "" for m in list(row)]
Id = str(rowItems[0])
try:
self.lastId = int(Id)
except ValueError:
pass
# XXX: review all for bytes usage
if isinstance(rowItems[1], bytes):
url = str(rowItems[1], "utf-8", "ignore")
else:
url = str(rowItems[1])
status = str(rowItems[2])
response_headers = str(rowItems[3])
request_headers = str(rowItems[4])
# TODO: make configurable
if status in ("400", "404", "500", "501"):
continue
# TODO:
m = self.re_set_cookie.search(response_headers)
if m:
setCookies = m.group(1)
cookieList = QNetworkCookie.parseCookies(setCookies)
global_cookie_jar.setCookiesFromUrl(cookieList, QUrl.fromEncoded(url))
parsed = urlparse.urlsplit(url)
hostname = ""
#.........这里部分代码省略.........
示例3: SVDViewer
# 需要导入模块: from PyQt4.QtCore import QMutex [as 别名]
# 或者: from PyQt4.QtCore.QMutex import tryLock [as 别名]
#.........这里部分代码省略.........
mouse = np.int32([self.mouseX, self.mouseY])
offsets = self.screenpts - mouse
return np.sqrt(np.sum(offsets*offsets, axis=1))
def get_nearest_point(self):
return np.argmin(self.distances_from_mouse(self.array))
def select_nearest_point(self):
self.selected_index = self.get_nearest_point()
self.selectEvent(self.selected_index)
def selected_vector(self):
return self.array[self.selected_index]
def selected_label(self):
return self.labels[self.selected_index]
def selectEvent(self, index):
for layer in self.layers:
layer.selectEvent(index)
self.svdSelectEvent.emit()
def focus_on_point(self, text):
index = self.labels.index(text)
if index is None: return
coords = self.projection.components_to_projection(self.array[index])
if not self.is_point_on_screen(coords):
self.screen_center = coords
self.selected_index = index
self.selectEvent(index)
def paintEvent(self, event):
if self.paint_lock.tryLock():
QWidget.paintEvent(self, event)
self.painter.begin(self)
self.painter.setRenderHint(QPainter.Antialiasing, True)
self.painter.setRenderHint(QPainter.TextAntialiasing, True)
try:
for layer in self.layers:
layer.draw(self.painter)
finally:
self.painter.end()
self.paint_lock.unlock()
def resizeEvent(self, sizeEvent):
self.width = sizeEvent.size().width()
self.height = sizeEvent.size().height()
for layer in self.layers:
layer.resize(self.width, self.height)
def mouseMoveEvent(self, mouseEvent):
point = mouseEvent.pos()
self.mouseX = point.x()
self.mouseY = point.y()
for layer in self.layers: layer.mouseMoveEvent(mouseEvent)
if self.leftMouseDown() or self.rightMouseDown():
self.activate_timer()
elif self.timer_ticks >= TIMER_MAX:
self.update()
def timerEvent(self):
self.projection.timerEvent()
for layer in self.layers: layer.timerEvent()
self.update_screenpts()
self.update()
示例4: QuickAnalysisThread
# 需要导入模块: from PyQt4.QtCore import QMutex [as 别名]
# 或者: from PyQt4.QtCore.QMutex import tryLock [as 别名]
class QuickAnalysisThread(QThread):
def __init__(self, framework, parent = None):
QThread.__init__(self, parent)
self.framework = framework
self.callback_object = None
self.qtimer = QTimer()
self.qlock = QMutex()
QObject.connect(self, SIGNAL('quit()'), self.handle_quit)
QObject.connect(self, SIGNAL('started()'), self.handle_started)
self.Data = None
self.read_cursor = None
self.cursor = None
def db_attach(self):
self.Data = self.framework.getDB()
self.read_cursor = self.Data.allocate_thread_cursor()
def db_detach(self):
self.close_cursor()
self.Data = None
def close_cursor(self):
if self.read_cursor:
self.read_cursor.close()
self.Data.release_thread_cursor(self.read_cursor)
self.read_cursor = None
def run(self):
QObject.connect(self, SIGNAL('do_runQuickAnalysis()'), self.handle_runQuickAnalysis, Qt.DirectConnection)
self.exec_()
def runQuickAnalysis(self, python_code, callback_object):
self.python_code = python_code
self.callback_object = callback_object
QTimer.singleShot(50, self, SIGNAL('do_runQuickAnalysis()'))
def handle_quit(self):
self.framework.debug_log('QuickAnalysisThread quit...')
self.close_cursor()
self.exit(0)
def handle_started(self):
self.framework.debug_log('QuickAnalysisThread started...')
self.framework.subscribe_database_events(self.db_attach, self.db_detach)
def append_results(self, results_io, res):
if isinstance(res, bytes):
res = str(res, 'utf-8', 'ignore')
if res.endswith('\n'):
results_io.write(res + '\n')
else:
results_io.write(res + '\n\n')
def handle_runQuickAnalysis(self):
results = ''
results_io = StringIO()
if not self.qlock.tryLock():
self.framework.debug_log('failed to acquire lock for quick analysis')
else:
original_stdout = sys.stdout
sys.stdout = results_io
try:
python_code = str(self.python_code)
scriptLoader = ScriptLoader()
global_ns = local_ns = {}
script_env = scriptLoader.load_from_string(python_code, global_ns, local_ns)
begin_method = script_env.functions.get('begin')
if begin_method:
res = begin_method()
if res:
self.append_results(results_io, res)
process_request_method = script_env.functions.get('process_request')
if not process_request_method:
raise Exception('The "process_request" method is not implemented and is required.')
factory = RequestResponseFactory.RequestResponseFactory(self.framework, None)
for row in self.Data.read_all_responses(self.read_cursor):
try:
rr = factory.fill_by_row(row)
res = process_request_method(rr)
if res:
self.append_results(results_io, res)
except Exception as e:
results += '\nEncountered processing error: %s' % (e)
end_method = script_env.functions.get('end')
if end_method:
res = end_method()
if res:
self.append_results(results_io, res)
except Exception as error:
self.framework.report_exception(error)
results += '\nEncountered processing error: %s' % (error)
finally:
sys.stdout = original_stdout
self.qlock.unlock()
#.........这里部分代码省略.........
示例5: AnalyzerThread
# 需要导入模块: from PyQt4.QtCore import QMutex [as 别名]
# 或者: from PyQt4.QtCore.QMutex import tryLock [as 别名]
class AnalyzerThread(QThread):
def __init__(self, framework, parent = None):
QThread.__init__(self, parent)
self.framework = framework
self.callback_object = None
self.qtimer = QTimer()
self.qlock = QMutex()
QObject.connect(self, SIGNAL('quit()'), self.handle_quit)
QObject.connect(self, SIGNAL('started()'), self.handle_started)
self.Data = None
self.read_cursor = None
self.cursor = None
def db_attach(self):
self.Data = self.framework.getDB()
self.read_cursor = self.Data.allocate_thread_cursor()
self.cursor = self.Data.allocate_thread_cursor()
def db_detach(self):
self.close_cursor()
self.Data = None
def close_cursor(self):
if self.read_cursor:
self.read_cursor.close()
self.Data.release_thread_cursor(self.read_cursor)
self.read_cursor = None
if self.cursor:
self.cursor.close()
self.Data.release_thread_cursor(self.cursor)
self.cursor = None
def run(self):
QObject.connect(self, SIGNAL('do_runAnalysis()'), self.handle_runAnalysis, Qt.DirectConnection)
self.exec_()
def runAnalysis(self, callback_object):
self.callback_object = callback_object
QTimer.singleShot(50, self, SIGNAL('do_runAnalysis()'))
def handle_quit(self):
self.framework.debug_log('AnalyzerThread quit...')
self.close_cursor()
self.exit(0)
def handle_started(self):
self.framework.debug_log('AnalyzerThread started...')
self.framework.subscribe_database_events(self.db_attach, self.db_detach)
def handle_runAnalysis(self):
fullanalysistext = ''
if not self.qlock.tryLock():
self.framework.debug_log('failed to acquire lock for analyzers')
else:
try:
fullanalysistext = self.analyze_content()
except Exception as e:
self.framework.report_exception(e)
finally:
self.qlock.unlock()
if self.callback_object:
self.callback_object.emit(SIGNAL('runAnalysisFinished(QString)'), str(fullanalysistext))
def result_type_to_string(self,resulttype):
"""Reads type and package information from a result, and returns it as a string"""
resultclass=resulttype.__class__
return "".join((resultclass.__module__,".",resultclass.__name__))
def analyze_content(self):
""" Perform analysis on the captured content"""
#TODO: NEW DB THREAD TO HOLD RESPONSES, ANOTHER FOR WRITING RESULTS
scopeController = self.framework.getScopeController()
response = self.Data.read_all_responses(self.read_cursor)
response_IDs = []
for row in response:
dbrow = [m or '' for m in row]
Id = dbrow[ResponsesTable.ID]
url = dbrow[ResponsesTable.URL]
if scopeController.isUrlInScope(url, url):
response_IDs.append(Id)
#Instantiate all found analyzers
analyzerobjects = AnalyzerList(self.framework)
analyzerobjects.instantiate_analyzers()
analysisrunid=self.Data.analysis_start(self.cursor)
#TODO - Consider threading from here down
for x in analyzerobjects:
#dbconfig=self.Data.get_config_value(self.read_cursor, 'ANALYSIS', str(x.__class__))
#print "dbconfig=%s"%dbconfig
print("class=%s"%x.__class__)
#x.setConfiguration(dbconfig)
x.preanalysis()
#.........这里部分代码省略.........