muti_thread是多线程并发库
Project description
muti_thread是一个真正的多并发多线程,python3.6
# -*- coding: utf-8 -*-
import sys
sys.path.append(r'E:\algo_text\nn_csdk\build\Release')
import time
from muti_thread import thread_create,thread_append,thread_start_and_join
import numpy as np
vec1 = [1,2,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
vec2 = [5,6,7,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
def my_compute(n=10000):
for i in range(n):
dist1 = np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))
class thread_param:
def __init__(self,index):
self.counter = 0
self.index = index
#必须有且只有一个参数,最好是某个类的实例
#注意 , 线程函数里最好不要有import的东西 ,里面最好是执行函数
def thread_func(param):
#param.counter += 1
#print(param.index)
#测试1万次
my_compute()
# if param.counter> 10000:
# print(param.index ,'end')
# return -1
# ii = -1
# while ii < 10:
# ii += 1
# print('线程', param.index, ii)
#
# time.sleep(1)
#print(param.index, 'end')
return -1 #// 返回值小于0 退出线程 , 0 继续循环执行线程函数
def main():
resource = thread_create()
#并发线程
for i in range(10):
param = thread_param(i)
thread_append(resource, thread_func, param)
#开始并等待线程结束
start = float(round(time.time() * 1000))
thread_start_and_join(resource)
end = float(round(time.time() * 1000))
print('run time {}'.format (end-start))
if __name__ == '__main__':
main()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for muti-thread-1.1.9.linux-x86_64.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e00e02a9a432898178972c508688902f659021cbb08737d92b947f817af8d538 |
|
MD5 | 9c9713771883b1cec715566821ec6cdc |
|
BLAKE2b-256 | 09844fabe1d973f71c17f98a18a07af33e88e9d556c49324adb6ab78ead3d92c |
Close
Hashes for muti_thread-1.1.9-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11b8a77ac728f1ebdc798ed10cdbde3ac703fbe666d9c5f821809d176a184671 |
|
MD5 | 0768e7f261619e48aace5a4c70ad1d40 |
|
BLAKE2b-256 | 9b62a4590547205ea6622ff4cd6599082969a3d8d89e188f54144189f3cf49b5 |