为什么千瓦的值不同

2024-09-25 06:28:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个函数来处理协程异常和中断,logfunc用来记录中断前任务的进度

def async_process(task,logfunc=None,**kwargs):
    loop = asyncio.get_event_loop()

    try:
        # loop.set_debug(True)

        return loop.run_until_complete(task(**kwargs))
    except KeyboardInterrupt as e:
        print('KeyboardInterrupt')
    finally:
        try:
            all_tasks = asyncio.gather(*asyncio.all_tasks(loop), return_exceptions=True)
            all_tasks.cancel()
            with contextlib.suppress(asyncio.CancelledError):
                loop.run_until_complete(all_tasks)
            loop.run_until_complete(loop.shutdown_asyncgens())
        finally:
            # save_last_id(last_id, **kwargs)
            logfunc
            loop.close()

但是当我这样打电话的时候

for kw in rules_options:
            async_process(key_processing, logfunc=save_last_id(last_id, kw, 'webinfo'), load_id=load_id(kw,'webinfo'), kw=kw)

保存\上次\ id中传递的kw值与键\处理中传递的kw值不相同? 为什么?

附加:

关于变量规则\u选项

configer.read("rule.txt")
rules_options = configer.options("rules")

我的“rule.txt”中的内容如下:

[rules]
catg=key1,key2,key3

关于保存最后一个id函数和加载id

def save_last_id(id,kw,table_name):
    connection = sqlite3.connect('log.db', check_same_thread=False)
    configer = configparser.ConfigParser()
    configer.read(config)
    rule = configer.get("rules", kw)

    if id != "-1" or id is not None :
        connection.execute("insert or replace into {} (key,rule,last_id) values ('{}','{}','{}')".format(table_name, kw, rule, id))
        connection.commit()
    connection.close()

def load_id(kw,table_name):
    connection = sqlite3.connect('log.db', check_same_thread=False)
    connection.executescript("""
            CREATE TABLE IF NOT EXISTS {}(
                key TEXT NOT NULL,
                rule TEXT NOT NULL PRIMARY KEY,
                last_id TEXT
            )
            """.format(table_name)
                            )
    try:
        configer = configparser.ConfigParser()
        configer.read(config)
        rule = configer.get("rules", kw)
        print("select last_id from {} where rule = '{}' ".format(table_name, rule))
        last_id = connection.execute("select last_id from {} where rule = '{}' ".format(table_name, rule)).fetchone()

        if last_id is None:
            return -1
        elif len(last_id) == 0:
            return -1
        else:
            return last_id[0]
    except Exception as e:
        print(e)
        return -1

Tags: nameloopasyncioidreturntableallconnection