I am developing desktop software based on:
- Python: PySide (WebKit)
- JavaScript: AngularJS
Now, I need implement async feature:
- while uploading images from FTP (Python part) I need to show some uploader on UI (HTML+JS)
Need your advices about the elegant solution.
Very simple solution:
- save some flags in Python dictionary
- request value of this flags in JS by timer :)
However, I need to hard code the time and once I have long operation such as downloading files from FTP, its issue for me
Code of the solution:
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
@QtCore.Slot(int, result=str)
def get_new_templates_list(self, id):
thread.start_new_thread(get_new_templates_list,
(id,))
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
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 ''