关于多线程的,朋友帮忙写了一个,但是还不是十分懂,要放假 不想学了,先摆在这里叭
还有一篇文章,写得挺好的,放这里回头看;
from multiprocessing.pool import ThreadPool
import random
import time
def year():
# '''生成年份'''
# 1978为第一代身份证执行年份,now-18直接过滤掉小于18岁出生的年份
now = time.strftime('%Y') #生成年份
second = random.randint(1961, 1988)
# age = int(now)-second
# print('随机生成的身份证人员年龄为:'+str(age))
return second
def month():
# '''生成月份'''
three = random.randint(1, 12)
if three < 10:
three = '0' + str(three)
return three
else:
return str(three)
def worker(start, end):
result = []
for i in range(start, end):
y = year()
m = month()
d = day()
ld = getDay(y, m)
result.append([y, m])
return result
def multiprocessing(num):
# 需要分成多少个线程
thread_num = 10
results = []
pool = ThreadPool(processes=thread_num)
for i in range(thread_num):
start = i * num // thread_num
end = (i + 1) * num // thread_num
if(i == thread_num - 1):
end = num
results.append(pool.apply_async(worker, (start, end)))
pool.close()
pool.join()
return results
if __name__ == "__main__":
num = 6000000
start = time.time()
results = multiprocessing(num)
# for res in results:
# res = res.get()
# print(len(res))
# for k in res:
# # wirte_to_file()
# print(k)
print(results[0].get()[0])
end = time.time()
print(end - start)