最新消息:20210917 已从crifan.com换到crifan.org

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

Python crifan 2617浏览 0评论

折腾:

【部分解决】Python中实现多线程或多进程中的单例singleton

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

注:

后来通过

【已解决】Flask的gunicorn中多进程多worker如何共享数据或单实例

才基本上明白相关的逻辑和概念的:

  • process=进程:相对最消耗资源

    • 而gunicorn中的worker,应该就是process

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

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

先要去搞清楚如何创建:

python 多线程

Python 多线程 | 菜鸟教程

多线程 – 廖雪峰的官方网站

然后用:

<code>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()
</code>

测试多线程输出的结果中,是可以实现单例的:

<code>[2018-08-29 17:59:51,835 INFO tasks.py:125 &lt;module&gt;] ========== test multiple thread singleton ==========
[2018-08-29 17:59:51,836 INFO tasks.py:140 &lt;module&gt;] [1] T1, &lt;Thread(T1, initial)&gt;
[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=&lt;_MainThread(MainThread, started 140735952733056)&gt;, curThreadTokenSingleton=&lt;resources.tasks.MsTtsTokenSingleton object at 0x10804c4a8&gt;
[2018-08-29 17:59:55,545 INFO tasks.py:140 &lt;module&gt;] [2] T2, &lt;Thread(T2, initial)&gt;
[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=&lt;_MainThread(MainThread, started 140735952733056)&gt;, curThreadTokenSingleton=&lt;resources.tasks.MsTtsTokenSingleton object at 0x10804c4a8&gt;
[2018-08-29 18:00:22,057 INFO tasks.py:140 &lt;module&gt;] [3] T3, &lt;Thread(T3, initial)&gt;
[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=&lt;_MainThread(MainThread, started 140735952733056)&gt;, curThreadTokenSingleton=&lt;resources.tasks.MsTtsTokenSingleton object at 0x10804c4a8&gt;
[2018-08-29 18:00:22,966 INFO tasks.py:140 &lt;module&gt;] [4] T4, &lt;Thread(T4, initial)&gt;
[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=&lt;_MainThread(MainThread, started 140735952733056)&gt;, curThreadTokenSingleton=&lt;resources.tasks.MsTtsTokenSingleton object at 0x10804c4a8&gt;
[2018-08-29 18:00:24,123 INFO tasks.py:140 &lt;module&gt;] [5] T5, &lt;Thread(T5, initial)&gt;
[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=&lt;_MainThread(MainThread, started 140735952733056)&gt;, curThreadTokenSingleton=&lt;resources.tasks.MsTtsTokenSingleton object at 0x10804c4a8&gt;
</code>

【总结】

至此,

【部分解决】Python中实现多线程或多进程中的单例singleton

中的:

ThreadSafeSingleton

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

转载请注明:在路上 » 【已解决】Python中用多线程thread去测试单例Singleton

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.193 seconds, using 23.41MB memory