Example #1
0
class LongCalculation(QProgressDialog):
    """
    Multiprocessing based worker for mesh and eigenvalue calculations.

    This is necessary to make sure GUI is not blocked while mesh is built,
    or when eigenvalue calculations are performed.

    Transformations do not need as much time, unless there is one implemented
    without numpy vectorized coordinate calculations.
    """

    res = None

    def __init__(self, fun, args, postprocess, job):
        """ Build multiprocessing queues and start worker. """
        super(LongCalculation, self).__init__(job, "Cancel", 0, 0)
        self.setModal(True)
        self.input = Queue()
        self.output = Queue()
        self.input.put((fun, args, postprocess))
        self.proc = Process(target=worker, args=(self.input, self.output))
        self.proc.start()
        self.timer = QTimer()
        self.timer.timeout.connect(self.update)
        self.timer.start(10)

    def update(self):
        """ Check if worker is done, and close dialog. """
        try:
            out = self.output.get(block=False)
            if isinstance(out, basestring):
                self.setLabelText(out)
                return
            if out is None:
                self.done(0)
                return
            self.res = out
            self.timer.stop()
            self.proc.join()
            del self.proc
            self.done(1)
        except:
            pass

    def cleanUp(self):
        """ Kill the running processes if cancelled/failed. """
        if self.proc:
            while self.proc.is_alive():
                self.proc.terminate()
            del self.proc
        self.timer.stop()
Example #2
0
        proxies = {
            p[2].lower(): '%s:%s' % (p[0], p[1]),
        }
        try:
            begin = time.time()
            code = requests.get("http://www.google.com.hk/",
                                proxies=proxies,
                                timeout=5).status_code
            cost = int((time.time() - begin) * 1000)
            all.append(list(p) + [
                cost,
            ])
            logger.info("%s,%s,%s" % (p, code, cost))
        except:
            pass
            # logger.error("%s,%s" % (p, 'error'))


if __name__ == "__main__":
    pages = get_list_page()
    proxys = scra_list_page(pages)
    for p in proxys:
        queue.put(p)
    ths = [threading.Thread(target=test_proxy) for i in xrange(40)]
    for t in ths:
        t.start()
    for t in ths:
        t.join()

    print sorted(p, key=lambda x: x[-1])