def python_multi(n)_python multi process multi thread

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

# 新线程执行的代码:

def loop():

print'thread %s is running...' %threading.current_thread().name

n= 0

while n < 5:

n= n + 1print'thread %s >>> %s' %(threading.current_thread().name, n)

time.sleep(1)

print'thread %s ended.' %threading.current_thread().name

print'thread %s is running...' %threading.current_thread().name

t= threading.Thread(target=loop, name='LoopThread')

t.start()

t.join()

print'thread %s ended.' % 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})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值