简体   繁体   中英

JavaScript and async methods in QtWebKit

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 :

  1. save some flags in Python dictionary
  2. 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 ''

Here is what you can do:

  1. retrieve the file size with stat / stat.ST_SIZE ,
  2. decide on a chunk size,
  3. call 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.
  4. pass a callback to ftplib.FTP.storbinary() or ftplib.FTP.storlines() so that each block / line transferred increases your progress bar accordingly with PySide.QtGui.QProgressBar.setValue()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM