Python threading.thread() Examples

The following are code examples for showing how to use threading.thread(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: icrawler   Author: hellock   File: proxy_pool.py    MIT License 5 votes vote down vote up
def validate(self,
                 proxy_scanner,
                 expected_num=20,
                 queue_timeout=3,
                 val_timeout=5):
        """Target function of validation threads

        Args:
            proxy_scanner: A ProxyScanner object.
            expected_num: Max number of valid proxies to be scanned.
            queue_timeout: Timeout for getting a proxy from the queue.
            val_timeout: An integer passed to `is_valid` as argument `timeout`.
        """
        while self.proxy_num() < expected_num:
            try:
                candidate_proxy = proxy_scanner.proxy_queue.get(
                    timeout=queue_timeout)
            except queue.Empty:
                if proxy_scanner.is_scanning():
                    continue
                else:
                    break
            addr = candidate_proxy['addr']
            protocol = candidate_proxy['protocol']
            ret = self.is_valid(addr, protocol, val_timeout)
            if self.proxy_num() >= expected_num:
                self.logger.info('Enough valid proxies, thread {} exit.'
                                 .format(threading.current_thread().name))
                break
            if ret['valid']:
                self.add_proxy(Proxy(addr, protocol))
                self.logger.info('{} ok, {:.2f}s'.format(addr, ret[
                    'response_time']))
            else:
                self.logger.info('{} invalid, {}'.format(addr, ret['msg'])) 
Example 2
Project: icrawler   Author: hellock   File: proxy_pool.py    MIT License 5 votes vote down vote up
def is_scanning(self):
        """Return whether at least one scanning thread is alive"""
        for t in self.scan_threads:
            if t.is_alive():
                return True
        return False 
Example 3
Project: icrawler   Author: hellock   File: proxy_pool.py    MIT License 5 votes vote down vote up
def scan(self):
        """Start a thread for each registered scan function to scan proxy lists"""
        self.logger.info('{0} registered scan functions, starting {0} threads '
                         'to scan candidate proxy lists...'
                         .format(len(self.scan_funcs)))
        for i in range(len(self.scan_funcs)):
            t = threading.Thread(
                name=self.scan_funcs[i].__name__,
                target=self.scan_funcs[i],
                kwargs=self.scan_kwargs[i])
            t.daemon = True
            self.scan_threads.append(t)
            t.start() 
Example 4
Project: 21datalab   Author: smartyal   File: widgets.py    MIT License 5 votes vote down vote up
def setup_logging(loglevel=logging.DEBUG,tag = ""):
    global haveLogger
    print("setup_logging",haveLogger)
    if not haveLogger:
        # fileName = 'C:/Users/al/devel/ARBEIT/testmyapp.log'
        #logging.basicConfig(format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', level=loglevel)

        #remove all initial handlers, e.g. console
        allHandlers = logging.getLogger('').handlers
        for h in allHandlers:
            logging.getLogger('').removeHandler(h)


        formatter = logging.Formatter('%(asctime)s %(name)-12s thid%(thread)d %(levelname)-8s %(message)s')
        console = logging.StreamHandler()
        console.setLevel(loglevel)
        console.setFormatter(formatter)
        logging.getLogger('').addHandler(console)

        #logfile = logging.FileHandler('./log/widget_'+'%08x' % random.randrange(16 ** 8)+".log")
        #logfile = logging.FileHandler('./widget_' + '%08x' % random.randrange(16 ** 8) + ".log")
        if tag == "":
            tag = '%08x' % random.randrange(16 ** 8)
        logfile = logging.FileHandler('./log/widget_' + tag+ ".log")
        logfile.setLevel(loglevel)
        logfile.setFormatter(formatter)
        logging.getLogger('').addHandler(logfile)
        haveLogger = True




#import model 
Example 5
Project: 21datalab   Author: smartyal   File: widgets.py    MIT License 5 votes vote down vote up
def __dispatch_function(self,function,arg=None):
        """
            queue a function to be executed in the periodic callback from the bokeh app main loop
            this is needed for functions which are triggered from a separate thread but need to be
            executed in the context of the bokeh app loop

        Args:
            function: functionpointer to be executed
        """
        with self.dispatchLock:
            self.logger.debug(f"__dispatch_function {function.__name__}, arg: {arg}")
            self.dispatchList.append({"function":function,"arg":arg}) 
Example 6
Project: wiicop   Author: barnabuskev   File: wiicop.py    GNU General Public License v3.0 5 votes vote down vote up
def onkeypress(self,evt):
        if evt.key==' ':
            # spacebar pressed
            if self.text_h.get_gid()=='notrec':
                # start recording data...
                # change colour of dot
                self.scat.set_facecolors('red')
                plt.draw()
                # set gid to recording to flag recording state
                self.text_h.set_gid('rec')
                if self.acq_info['acq_time'] != 'inf':
                    # timed acquisition - start timer
                    self.acq_timer.start()
                    self.text_h.set_text('Timed acquisition')
                else:
                    # manual acq
                    # change instructions
                    self.text_h.set_text(self.text_stop)
                # set thread to store data in queue
                lock.acquire()
                thd.storeflag = True
                lock.release()

            elif self.text_h.get_gid()=='rec':
                # stop recording
                if self.acq_info['acq_time'] != 'inf':
                    # timed acq - do nothing
                    pass
                else:
                    # recording data, manual acq
                    lock.acquire()
                    thd.storeflag = False
                    thd.runflag = False
                    lock.release()
                    plt.close()
            else:
                print('error in onkeypress - unrecognised text_h gid')

    # callback function for timer 
Example 7
Project: wiicop   Author: barnabuskev   File: wiicop.py    GNU General Public License v3.0 5 votes vote down vote up
def t_event(self):
        # stop thread queuing data and stop it running
        lock.acquire()
        thd.storeflag = False
        thd.runflag = False
        lock.release()
        self.acq_timer.remove_callback(self.t_event)
        plt.close()


# ~~~~~~~~~~~~~~~
# MAIN ROUTINE
# ~~~~~~~~~~~~~~~

# to suppress the annoying warning 
Example 8
Project: Quadcopter   Author: jonasrogert   File: sensor.py    GNU General Public License v2.0 5 votes vote down vote up
def run(self):
        #this function is called by the start function, inherit from threading.thread
        self.datalog = 'time'
        self.datalog += ';roll;pitch;yaw'
        self.datalog += ';r_rate;p_rate;y_rate'
        self.datalog += ';x_acc;y_acc;z_acc'
        self.datalog += '\n'

        initTime = time()
        currentTime = initTime
        counterPerf = 0  # for performance test
        self.logger.debug('IMU running...')
        while self.cycling:
            #cycling as fast as possible
            previousTime = currentTime
            currentTime = time()
            stepTime = currentTime - previousTime

            self.update(stepTime)

            if self.imulog is True:
                self.datalog += self.getDataString(stepTime, level=0)
                self.logger.debug(self.getDataString(stepTime, level=2))

            #used for performance test only
            doPerf = False
            if doPerf is True:
                counterPerf += 1
                if  counterPerf == 1000:
                    self.logger.info('1000 cycles time:' + str(currentTime - initTime))
                    doPerf = False

        self.logger.debug('IMU stopped') 
Example 9
Project: 21datalab   Author: smartyal   File: widgets.py    MIT License 4 votes vote down vote up
def stream_update(self):
        try:
            self.inStreamUpdate = True # to tell the range_cb that the range adjustment was not from the user
            self.logger.debug("stream update")#+str(self.streamingUpdateData))
            if self.streamingUpdateData:
                if not self.userZoomRunning:
                    if not self.streamingUpdateDataInterval == self.streamingInterval:
                        #the interval has changed in the meantime due to user pan/zoom, we skip this data, get fresh one
                        self.streamingUpdateData = None
                        self.inStreamUpdate = False
                        self.logger.debug("interval has changed")
                        return
                    #, we can now savely push them
                    # debug prints
                    #for k,v in self.streamingUpdateData.items():
                    #    self.logger.debug(f" {k}:{v}")

                    self.logger.debug(f"apply data {self.streamingUpdateData.keys()},")
                    if set(self.streamingUpdateData.keys()) != set(self.data.data.keys()):
                        self.logger.error(f"keys not match {self.streamingUpdateData.keys()},{self.data.data.keys()}, skip this data")
                        self.streamingUpdateData = None
                    else:
                        self.data.data = self.streamingUpdateData# #update the plot
                        self.logger.debug(f"streaming start {self.data.data['__time'][0]} end {self.data.data['__time'][-1]}, interv {self.streamingInterval}")
                        #self.plot.x_range.start = self.data.data["__time"][0]
                        #self.plot.x_range.end = self.data.data["__time"][-1]
                        if self.streamingMode:
                            #only in streaming Mode we set the axis new
                            self.set_x_axis(self.data.data["__time"][0],self.data.data["__time"][-1])
                        self.adjust_y_axis_limits()
                        if self.showBackgrounds:
                            #we also try to update the backgrounds here
                            self.stream_update_backgrounds()

                        self.streamingUpdateData = None #the thread can get new data
                else:
                    self.logger.info("user zoom running, try later")
                    #user is panning, zooming, we should wait and try again later
                    self.__dispatch_function(self.stream_update)
        except Exception as ex:
            self.logger.error(f"stream_update error {ex}")
        self.inStreamUpdate = False
        self.streamingUpdateData = None 
Example 10
Project: 21datalab   Author: smartyal   File: widgets.py    MIT License 4 votes vote down vote up
def __check_observed(self,counter):
        """
            this function is periodically called from a threading.thread
            we check if some data if the backend has changed and if we need to do something on change
        """
        self.logger.debug("enter __check_observed() "+str(counter))
        try:
            """
            #now see what we have to do
            if "background" in self.observerStatus:
                #check the background counter for update
                backgroundCounter = self.server.get_values(self.server.get_settings()["observer"]["observerBackground"])
                #self.logger.debug("background observer Val"+str(backgroundCounter))
                if self.observerStatus["background"] != None and self.observerStatus["background"] != backgroundCounter:
                    #we have a change in the background:
                    self.logger.debug("observer background changed")
                    self.__dispatch_function(self.refresh_backgrounds)
                self.observerStatus["background"] = backgroundCounter
            if "variables" in self.observerStatus:
                variables = self.server.get_selected_variables_sync()
                if self.observerStatus["variables"] != None and self.observerStatus["variables"]!=variables:
                    #we have a change in the selected variables
                    self.logger.debug("variables selection observer changed"+str(self.observerStatus["variables"] )+"=>"+str(variables))
                    self.__dispatch_function(self.refresh_plot)
                self.observerStatus["variables"] = variables
            """
            #now we also check if we have a legend click which means that we must delete a variable from the selection
            self.logger.debug("RENDERERS CHECK --------------------------")
            deleteList=[]
            for r in self.plot.renderers:
                if r.name and r.name in self.server.get_variables_selected() and r.visible == False:
                    #there was a click on the legend to hide the variables
                    self.logger.debug("=>>>>>>>>>>>>>>>>>DELETE FROM plot:"+r.name)
                    deleteList.append(r.name)
            if deleteList != []:
                #now prepare the new list:
                newVariablesSelected = [var for var in self.server.get_variables_selected() if var not in deleteList]
                self.logger.debug("new var list"+str(newVariablesSelected))
                self.server.set_variables_selected(newVariablesSelected)
                #self.__dispatch_function(self.refresh_plot)
        except Exception as ex:
            self.logger.error("problem during __check_observed"+str(ex)+str(sys.exc_info()[0]))

        self.logger.debug("leave __check_observed()") 
Example 11
Project: 21datalab   Author: smartyal   File: widgets.py    MIT License 4 votes vote down vote up
def periodic_cb(self):
        """
            called periodiaclly by the bokeh system
            here, we execute function that modifiy bokeh variables etc via the dispatching list
            this is needed, as modifications to data or parameters in the bokeh objects
            are only possible withing the bokeh thread, not from any other.

            attention: make sure this functin does normally not last longer than the periodic call back period, otherwise
            bokeh with not do anything else than this function here

        """
        if self.inPeriodicCb:
            self.logger.error("in periodic cb")
            return
        self.inPeriodicCb = True

        try:
            start = time.time()
            self.check_boxes()
            legendChange =  self.__legend_check() # check if a user has deselected a variable
            #try: # we need this, otherwise the inPeriodicCb will not be reset

            #self.logger.debug("enter periodic_cb")

            executelist=[]
            with self.dispatchLock:
                if self.dispatchList:
                    executelist = self.dispatchList.copy()
                    self.dispatchList = []

            for entry in executelist: # avoid double execution
                fkt = entry["function"]
                arg = entry["arg"]
                self.logger.info(f"now executing dispatched fkt {fkt.__name__} arg {arg}")
                if arg:
                    fkt(arg) # execute the functions which wait for execution and must be executed from this context
                else:
                    fkt()

        except Exception as ex:
            self.logger.error(f"Error in periodic callback {ex}")

        if legendChange or executelist != []:
            self.logger.debug(f"periodic_cb was {time.time()-start}")

        self.inPeriodicCb = False