python multi process multi thread

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python multi process multi thread相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%94%be%e5%bf%83' title='鏀惧績'>鏀惧績   name   final   col   one   tin   release   for   change   

muti thread:

python threading:

https://docs.python.org/2/library/threading.html#thread-objects

https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832360548a6491f20c62d427287739fcfa5d5be1f000

http://ebyerly.com/python-threading-examples.html

recommend to use coroutine+muti process to replace muti thread in python

https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868328689835ecd883d910145dfa8227b539725e5ed000

basic:

import time, threading

# 鏂扮嚎绋嬫墽琛岀殑浠g爜:
def loop():
    print 鈥?/span>thread %s is running...鈥?/span> % threading.current_thread().name
    n = 0
    while n < 5:
        n = n + 1
        print 鈥?/span>thread %s >>> %s鈥?/span> % (threading.current_thread().name, n)
        time.sleep(1)
    print 鈥?/span>thread %s ended.鈥?/span> % threading.current_thread().name

print 鈥?/span>thread %s is running...鈥?/span> % threading.current_thread().name
t = threading.Thread(target=loop, name=鈥?/span>LoopThread鈥?/span>)
t.start()
t.join()
print 鈥?/span>thread %s ended.鈥?/span> % threading.current_thread().name

lock:

balance = 0
lock = threading.Lock()

def run_thread(n):
    for i in range(100000):
        # 鍏堣鑾峰彇閿?
        lock.acquire()
        try:
            # 鏀惧績鍦版敼鍚?
            change_it(n)
        finally:
            # 鏀瑰畬浜嗕竴瀹氳閲婃斁閿?
            lock.release()

arugments:

import threading


def some_func(one, two, an_arg = 0):
    return one + two * an_arg


eg = threading.Thread(target=some_func,
                      args = (1, 2),
                      kwargs = {"an_arg": 3})

 

以上是关于python multi process multi thread的主要内容,如果未能解决你的问题,请参考以下文章

Multi-process Resource Loading

react-multi-carousel无法工作。

multi-cursor

Libcurl - curl_multi_wakeup

我在Multi-Processing中缺少什么?

找到了救命的东西 NVIDIA MPS (multi-process service)