Python閉包與多線程

来源:互联网 发布:34周b超数据判断男女 编辑:程序博客网 时间:2024/05/17 22:13

I had my position transferred for more than one week from Mobile Security to Clean Master, and have a chance to make a framework to transfer data from one database to another, also some annoying jobs are still in my hand.

Taking the benefits of the Internet and some my old projects, it's easy to frame a framework. Using multi-thread multi-process to do this.

I would like to share that a problem when I try to put an closure inside the thread pool.

code just like this.

def wrap():    a = [0]    def init_th():        using a here...        a[0] += 1    return init_thdef work(init_th):    init_th()    ....    returnthreading.Thread(target=work, arg=(wrap(), ))

As a result, it comes with the thread safety problem. needs a mutex for the init_th.

In fact, I suddenly know that the GIL never make the thread safety, it just remove some basic problems and make it safe in line-level.

原创粉丝点击