折腾:
期间,需要去想办法,写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,是可以实现单例的效果的。