[英]JavaScript and async methods in QtWebKit
I am developing desktop software based on: 我正在开发桌面软件基于:
Now, I need implement async feature: 现在,我需要实现异步功能:
Need your advices about the elegant solution. 需要您对优雅解决方案的建议。
Very simple solution : 很简单的解决方案
However, I need to hard code the time and once I have long operation such as downloading files from FTP, its issue for me 但是,我需要硬编码时间,一旦我有长时间的操作,如从FTP下载文件,它的问题对我来说
Code of the solution: 解决方案代码:
templateManager.js templateManager.js
function _loadNewTemplates() {
var deferred = $.Deferred();
asyncManager.run($.proxy( _loadNewTemplatesCallback, null, deferred ), "get_new_templates_list" );
return deferred;
}
function _loadNewTemplatesCallback ( deferred, response ) {
template.newList = response.result;
deferred.resolve();
}
app_mamager.py app_mamager.py
@QtCore.Slot(int, result=str)
def get_new_templates_list(self, id):
thread.start_new_thread(get_new_templates_list,
(id,))
utils.py utils.py
def get_new_templates_list(id):
config.set_response(id, json.dumps({'result': _get_new_templates_list(), 'error': None}, MyEncoder))
def _get_new_templates_list():
request = {'category': PRODUCT_CODE}
response = requests.post(URL,
data=request,
headers=HEADERS)
return json.loads(response.text)
config.py config.py
def set_response(id, request):
'''
Put pair (id, request) to the internal dictionary
:param id - int, request id:
:param request - string, request string:
:return:
'''
global _async_request
_async_request[id] = request
def get_response(id):
'''
Returns request or empty string if request does not exist
:param id - int
:return - string, request string:
'''
global _async_request
if _async_request.has_key(id):
return _async_request.pop(id)
else:
return ''
Here is what you can do: 这是你可以做的:
stat
/ stat.ST_SIZE
, 使用stat
/ stat.ST_SIZE
检索文件大小, chunk
size, 决定chunk
大小, PySide.QtGui.QProgressBar.setRange()
with the correct range, which is 0 to number of blocks ( filesize / ftp transfer chunk size ) for binary files or number of lines for text files. 使用正确的范围调用PySide.QtGui.QProgressBar.setRange()
,对于二进制文件或文本文件的行数,为0到块数(filesize / ftp传输块大小)。 ftplib.FTP.storbinary()
or ftplib.FTP.storlines()
so that each block / line transferred increases your progress bar accordingly with PySide.QtGui.QProgressBar.setValue()
将回调传递给ftplib.FTP.storbinary()
或ftplib.FTP.storlines()
以便传输的每个块/行相应地使用PySide.QtGui.QProgressBar.setValue()
相应地增加进度条
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.