Python閉包與多線程

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_th

def work(init_th):
    init_th()
    ....
    return

threading.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.

发布了57 篇原创文章 · 获赞 13 · 访问量 7万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览