Example #1
0
class Task():
    def __init__(self, name='default-task', logger=None):
        self.tasks = {}
        self.lock = threading.RLock()
        if logger:
            self.LOG = logger
        else:
            self.LOG = MyLogger(name + '.log', clevel=logging.DEBUG)
        self.need_stop = False

    def stop(self):
        self.need_stop = True
        self.LOG.warn('Thread %s stoped!' % (__name__))

    def add_task(self, name, func, run_times=1, interval=5, *argv):
        self.lock.acquire()
        if name and func and int(run_times) >= 1 and int(interval) >= 1:
            pass
        else:
            self.LOG.error("Invalid task: %s, run_times: %d, internal: %d" %
                           (name, int(run_times), int(interval)))
        self.LOG.info("To add task: %s, run_times: %d, internal: %d" %
                      (name, int(run_times), int(interval)))
        self.tasks[name] = {
            'func': func,
            'run_times': int(run_times),
            'interval': int(interval),
            'now_seconds': 0,
            'argv': argv,
            'state': 'active',
            'name': name
        }
        self.lock.release()

    def del_task(self, name):
        self.lock.acquire()
        self.LOG.warn("To delete task:%s" % (name))
        if name in self.tasks:
            del self.tasks[name]
        self.lock.release()

    def show_tasks(self):
        if self.tasks:
            for task in self.tasks:
                self.LOG.info(task + ":")
                for item in sorted(self.tasks[task]):
                    self.LOG.yinfo("    " + item.ljust(20) + ':' +
                                   str(self.tasks[task][item]).rjust(20))
        else:
            self.LOG.warn("No task...")

    def task_proc(self):
        while self.need_stop == False:
            if len(self.tasks) == 0:
                self.LOG.debug("No task!\n")
            '''
            for task in self.tasks:
                if self.tasks[task]['state'] == 'inactive':
                    self.del_task(task)
            '''
            try:
                self.lock.acquire()
                for task in self.tasks:
                    if self.tasks[task]['state'] != 'active':
                        continue
                    self.tasks[task]['now_seconds'] += 1
                    if self.tasks[task]['now_seconds'] >= self.tasks[task][
                            'interval']:
                        if callable(self.tasks[task]['func']):
                            # self.LOG.info("It is time to run %s: " % (
                            #    task) + self.tasks[task]['func'].__name__ + str(self.tasks[task]['argv']))
                            self.tasks[task]['func'](
                                *(self.tasks[task]['argv']))
                        elif callable(eval(self.tasks[task]['func'])):
                            # self.LOG.info("It is time to run %s: " % (
                            #    task) + self.tasks[task]['func'] + str(self.tasks[task]['argv']))
                            eval(self.tasks[task]['func'] + '(*' +
                                 str(self.tasks[task]['argv']) + ')')
                        else:
                            self.LOG.error(
                                "Uncallable task: %s, will disable it!")
                            self.tasks[task]['state'] = 'inactive'
                        self.tasks[task]['now_seconds'] = 0
                        self.tasks[task]['run_times'] -= 1
                        if self.tasks[task]['run_times'] == 0:
                            self.LOG.info("stop task:%s" % (task))
                            self.tasks[task]['state'] = 'inactive'
                    else:
                        pass
                self.lock.release()
                time.sleep(0.1)

            except RuntimeError:
                pass
Example #2
0
        "MzAxJh8VJ0iYjj79FZiMdUcNJmiECUuJJziUIlbFFZiEBl4VJmiEfmSZJyiImibJFlh8hzYxFVh0Z29lFDhw0m6lJZiAcIEtFThw4Us9FVh044QVFoh0DqV5FohYJJXRFRh1CbOVFFhky6mFJbhcXsCVFRh15J6BFGhoT9nZJHhwQr9RFnhkltGFJohcMs/xJjhIpb/lFkhILtnFFVh5VuDRFfh8TQtiFYiVxhFFFUhs4tJJFrhoNpQ5FThsXZ9xFWhg4expFZh4vwieFFiANPxZFVh9uBxeJZiRutF1FEiAxN5lJEiRSQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABMA4BUA0NgPCTNfENKcDQ4iMQIiMQ4ktlQBsNoRXsMiEDGkH+FGchDReQpwKnAAkswVljkcI2G+Hl2yUANQsxLk8G4RIyML4S1hAEKiBX5aVTGyvR02rRAAQOQEBgwdAUF1GVb4YSGaeRgus01A5OkCCW5SQyDpAXU9VSFgwgzjkzAwA3gTTwAAAAAAAAA=",

        "MzAxIRxlFUiY3R7ZFVic5TepFVigZjVNJnisAV99JmiUeXMVFoiU7oOZFWiE44W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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABMAy3IiY5MJcURTIgLBERoEWhDgqR3XEnJRETcO5h0jATN1Ei5GbjPgeAeLy1wiQKQcWq4KITDjH2LgGAAQliJHUhMC4+0NIJcZIaHKG9TRDiEAJAVyOwEQYVIPXZQMACGWIWMlFASy1wQLOSYAwYomNokZAKCLJFhvSRAh7RUqDiUBQHEj2QAAAAAAAAA="
    ]

    # get all pics
    all_pics = os.listdir(picpath)
    random.shuffle(all_pics)
    i = 0
    rs = []
    for pic in all_pics * 10:
        if i >= arg_handle.get_args('count'):
            break
        LOG.debug('create info for pic: %s' % pic)
        r = copy.deepcopy(msg)
        with open(picpath + os.path.sep + pic, "rb") as f:
            pic_str = base64.b64encode(f.read()).decode('utf-8')
        pic_type = 'jpeg'
        id = '%04d' % i
        r["name"] = "小狗狗" + id
        r["idenNum"] = "21122319850606" + id
        r["phone"] = "1888885" + id
        r["fileName"] = r["name"] + '.jpg'
        r["facePicBase64"] = "data:image/" + \
            pic_type + ';base64,' + pic_str
        r["fingerCode1"] = fingerCode_list[random.randint(
            0, len(fingerCode_list) - 1)]
        i += 1
        rs.append(r)