Ejemplo n.º 1
0
def find_db():
    thread = ct()
    try:
        ret = DALS[thread]
        # if first_db == current.db
        # future_commit()
        ret._adapter.reconnect()
    except KeyError:
        fdb = first_db
        ret = DALS[thread] = oDAL(DAL_ARGS[0], *DAL_ARGS[1], **DAL_ARGS[2])
        if fdb.tables:
            for tab in attrgetter(*fdb.tables)(fdb):
                setattr(ret, tab._tablename, tab)
        else:
            print 'EEEEE : vecchio DB senza tabelle'
        ret._tables = fdb._tables
    return ret
Ejemplo n.º 2
0
def call_back2(obj):
    print("<==回调函数的线程号{}==>".format(ct().ident))
    print(obj.result())
Ejemplo n.º 3
0
def func2(i):
    time.sleep(random.uniform(0.1, 0.9))
    print(" 线程任务执行中 ...  start ... 线程号{}".format(ct().ident), i)
    print(" 线程任务执行中 ...  end ... 线程号{}".format(ct().ident))
    return i
Ejemplo n.º 4
0
    # p.shutdown()
    # print(   "主进程执行结束...进程号:"    ,    os.getpid()  )
    # """

    print("<==============================================>")

    # (2)线程池  结果:(线程池的回调函数由子线程执行)
    t = ThreadPoolExecutor()
    for i in range(1, 11):
        obj = t.submit(func2, i)
        # 使用add_done_callback在获取最后返回值的时候,可以异步并发
        obj.add_done_callback(call_back2)
        # 直接使用result获取返回值的时候,会变成同步程序,速度慢;
        # obj.result()
    t.shutdown()
    print("主线程执行结束 .... 线程号{}".format(ct().ident))


# """
# 原型:
class Ceshi():
    def add_done_callback(self, func):  #2中间函数(库函数) 回调函数的调用者
        print("系统执行操作1 ... ")
        print("系统执行操作2 ... ")
        # 回头调用一下  调用回调函数  扩展和修改中间函数的功能
        func(self)

    def result(self):
        return 112233

Ejemplo n.º 5
0
def set_db(db):
    DALS[ct()] = db
Ejemplo n.º 6
0
def call_back2(obj):
    print('<==回调函数的线程号{}==>'.format(ct().ident))
    print(obj.result())


if __name__ == '__main__':
    # (2)线程池  结果:(线程池的回调函数由子线程执行)
    t = ThreadPoolExecutor()  # 2个cpu  2*5=10个子线程  10个任务  linux
    for i in range(1, 11):
        obj = t.submit(func2, i)  # 子线程 t = Thread(target=func2,args=(i,))
        # 使用add_done_callback在获取最后返回值的时候,可以异步并发
        obj.add_done_callback(call_back2)
        # 回调函数当参数传入
        # 直接使用result获取返回值的时候,会变成同步程序,速度慢;
        obj.result()  #类似for内join的用法

    t.shutdown()  #所有的子线程执行完毕后,才放行下面的代码
    # 类似join的用法
    print('主线程执行结束 .... 线程号{}'.format(ct().ident))

# 线程任务执行中 ...  start ... 线程号140316951013120 9
# 线程任务执行中 ...  end ... 线程号140316951013120
# <==回调函数的线程号140316951013120==>
# 9
# 线程任务执行中 ...  start ... 线程号140316967798528 10
# 线程任务执行中 ...  end ... 线程号140316967798528
# <==回调函数的线程号140316967798528==>
# 10
# 主线程执行结束 .... 线程号140317035034368
Ejemplo n.º 7
0
def call_back2(obj):   #参数是对象
	print(   "<==回调函数的线程号{}==>".format(  ct().ident) )
	print(obj.result())  #异步获取返回值
Ejemplo n.º 8
0
def func(i):
	time.sleep(random.uniform(0.1,0.7))
	print("thread ... 线程号{}".format(ct().ident),i)
	# 注意:ct().ident )如果写错了,pycharm没有报错提示
	return "*" * i
Ejemplo n.º 9
0
def func(i):
	print(" 任务执行中 ...  start ... 线程号{}".format( ct().ident ) , i )
	# 注意:ct().ident )如果写错了,pycharm没有报错提示
	time.sleep(1)
	print(" 任务执行中 ...  end ... 线程号{}".format(ct().ident ) , i)
	return ct().ident  # 线程号
Ejemplo n.º 10
0
def func(i):
	time.sleep(random.uniform(0.1,0.8))
	print('thread ... 线程号{}'.format(ct().ident),i)
	# 注意:ct().ident )如果写错了,pycharm没有报错提示
	return '*' * i