python线程锁 单例_【已解决】Python中用多线程thread去测试单例Singleton

折腾:

期间,需要去想办法,写Python的多线程thread去测试前面的ThreadSafeSingleton,是否真的能实现单例的效果。

注:

后来通过

才基本上明白相关的逻辑和概念的:process=进程:相对最消耗资源而gunicorn中的worker,应该就是process

thread=线程:(内存等)资源消耗比process小

coroutine=协程:资源消耗比thread小

先要去搞清楚如何创建:

python 多线程

然后用:log.info("========== test multiple thread singleton ==========")

import time, threading

def singleThreadDo():

log.info("---------- singleThreadDo ----------")

cur_thread = threading.current_thread()

cur_thread_name = cur_thread.name

curThreadTokenSingleton = MsTtsTokenSingleton()

log.info("[%s] cur_thread=%s, curThreadTokenSingleton=%s", cur_thread_name, cur_thread, curThreadTokenSingleton)

max_thread_num = 5

for idx in range(max_thread_num):

cur_num = idx + 1

each_thread_name = "T%s" % cur_num

cur_thread = threading.Thread(target=singleThreadDo, name=each_thread_name)

log.info("[%d] %s, %s", cur_num, each_thread_name, cur_thread)

# cur_thread.start()

cur_thread.run()

测试多线程输出的结果中,是可以实现单例的:[2018-08-29 17:59:51,835 INFO tasks.py:125 <module>] ========== test multiple thread singleton ==========

[2018-08-29 17:59:51,836 INFO tasks.py:140 <module>] [1] T1, <Thread(T1, initial)>

[2018-08-29 17:59:55,541 INFO tasks.py:129 singleThreadDo] ---------- singleThreadDo ----------

[2018-08-29 17:59:55,543 INFO tasks.py:133 singleThreadDo] [MainThread] cur_thread=<_MainThread(MainThread, started 140735952733056)>, curThreadTokenSingleton=<resources.tasks.MsTtsTokenSingleton object at 0x10804c4a8>

[2018-08-29 17:59:55,545 INFO tasks.py:140 <module>] [2] T2, <Thread(T2, initial)>

[2018-08-29 18:00:22,054 INFO tasks.py:129 singleThreadDo] ---------- singleThreadDo ----------

[2018-08-29 18:00:22,056 INFO tasks.py:133 singleThreadDo] [MainThread] cur_thread=<_MainThread(MainThread, started 140735952733056)>, curThreadTokenSingleton=<resources.tasks.MsTtsTokenSingleton object at 0x10804c4a8>

[2018-08-29 18:00:22,057 INFO tasks.py:140 <module>] [3] T3, <Thread(T3, initial)>

[2018-08-29 18:00:22,963 INFO tasks.py:129 singleThreadDo] ---------- singleThreadDo ----------

[2018-08-29 18:00:22,964 INFO tasks.py:133 singleThreadDo] [MainThread] cur_thread=<_MainThread(MainThread, started 140735952733056)>, curThreadTokenSingleton=<resources.tasks.MsTtsTokenSingleton object at 0x10804c4a8>

[2018-08-29 18:00:22,966 INFO tasks.py:140 <module>] [4] T4, <Thread(T4, initial)>

[2018-08-29 18:00:24,122 INFO tasks.py:129 singleThreadDo] ---------- singleThreadDo ----------

[2018-08-29 18:00:24,123 INFO tasks.py:133 singleThreadDo] [MainThread] cur_thread=<_MainThread(MainThread, started 140735952733056)>, curThreadTokenSingleton=<resources.tasks.MsTtsTokenSingleton object at 0x10804c4a8>

[2018-08-29 18:00:24,123 INFO tasks.py:140 <module>] [5] T5, <Thread(T5, initial)>

[2018-08-29 18:00:25,880 INFO tasks.py:129 singleThreadDo] ---------- singleThreadDo ----------

[2018-08-29 18:00:25,881 INFO tasks.py:133 singleThreadDo] [MainThread] cur_thread=<_MainThread(MainThread, started 140735952733056)>, curThreadTokenSingleton=<resources.tasks.MsTtsTokenSingleton object at 0x10804c4a8>

【总结】

至此,

中的:

ThreadSafeSingleton

对于多个线程thread,是可以实现单例的效果的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值