20.multi_case03
# 多线程
import threading
import time
class myThread(threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print("Starting " + self.name)
# 获得锁,成功获得锁定后返回True
# 可选的timeout参数不填时将一直阻塞直到获得锁定
# 否则超时后将返回False
threadLock.acquire()
print_time(self.name, self.counter, 3)
# 释放锁
threadLock.release()
def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1
threadLock = threading.Lock()
threads = []
# 创建新线程
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
# 开启新线程
thread1.start()
thread2.start()
# 添加线程到线程列表
threads.append(thread1)
threads.append(thread2)
# 等待所有线程完成
for t in threads:
t.join()
print("Exiting Main Thread")
import threadpool
import time
def sayhello(a):
print("hello: " + a)
time.sleep(2)
def main():
global result
seed = ["a", "b", "c"]
start = time.time()
task_pool = threadpool.ThreadPool(5)
requests = threadpool.makeRequests(sayhello, seed)
for req in requests:
task_pool.putRequest(req)
task_pool.wait()
end = time.time()
time_m = end - start
print("time: " + str(time_m))
start1 = time.time()
for each in seed:
sayhello(each)
end1 = time.time()
print("time1: " + str(end1 - start1))
if __name__ == '__main__':
main()
from concurrent.futures import ThreadPoolExecutor
import time
def sayhello(a):
print("hello: " + a)
time.sleep(2)
def main():
seed = ["a", "b", "c"]
start1 = time.time()
for each in seed:
sayhello(each)
end1 = time.time()
print("time1: " + str(end1 - start1))
start2 = time.time()
with ThreadPoolExecutor(3) as executor:
for each in seed:
executor.submit(sayhello, each)
end2 = time.time()
print("time2: " + str(end2 - start2))
start3 = time.time()
with ThreadPoolExecutor(3) as executor1:
executor1.map(sayhello, seed)
end3 = time.time()
print("time3: " + str(end3 - start3))
if __name__ == '__main__':
main()
20.multi_case03的更多相关文章
- CSharpGL(20)用unProject和Project实现鼠标拖拽图元
CSharpGL(20)用unProject和Project实现鼠标拖拽图元 效果图 例如,你可以把Big Dipper这个模型拽成下面这个样子. 配合旋转,还可以继续拖拽成这样. 当然,能拖拽的不只 ...
- ABP(现代ASP.NET样板开发框架)系列之20、ABP展现层——动态生成WebApi
点这里进入ABP系列文章总目录 ABP(现代ASP.NET样板开发框架)系列之20.ABP展现层——动态生成WebApi ABP是“ASP.NET Boilerplate Project (ASP.N ...
- 帮我做个APP,给你20万,做不做?
一.为什么要写这篇文章 前段时间,有个辞职 创业的同事(做法务的) 问我 开发一个 新闻类的APP要多少钱,产品.UI.接口.后台管理页 他们啥都没有,想全部外包. 我 并没有在外包公司做过,也没 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(20)-权限管理系统-根据权限获取菜单
系列目录 不知不觉到20讲,真是漫长的日子,可惜最近工作挺忙,要不可以有更多的时间来更新,多谢大家的一路支持.如果你觉得好,记得帮我点击推荐^-^ 我们在之前已经插入一些真实数据,其中包含了一个用户和 ...
- LINQ to SQL语句(20)之存储过程
在我们编写程序中,往往需要一些存储过程,在LINQ to SQL中怎么使用呢?也许比原来的更简单些.下面我们以NORTHWND.MDF数据库中自带的几个存储过程来理解一下. 1.标量返回 在数据库中, ...
- C#开发微信门户及应用(20)-微信企业号的菜单管理
前面几篇陆续介绍了很多微信企业号的相关操作,企业号和公众号一样都可以自定义菜单,因此他们也可以通过API进行菜单的创建.获取列表.删除的操作,因此本篇继续探讨这个主体,介绍企业号的菜单管理操作. 菜单 ...
- 20个非常有用的Java程序片段
下面是20个非常有用的Java程序片段,希望能对你有用. 1. 字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric strin ...
- 20个不可思议的 WebGL 示例和演示
WebGL 是一项在网页浏览器呈现3D画面的技术,有别于过去需要安装浏览器插件,通过 WebGL 的技术,只需要编写网页代码即可实现3D图像的展示.WebGL 可以为 Canvas 提供硬件3D加速渲 ...
- 20款 JavaScript 开发框架推荐给前端开发者
下面,我们给大家提供了一个用于 HTML5 开发的各种用途的 JavaScript 库列表.这些框架能够给前端开发人员提供更好的功能实现的解决方案.如果你有收藏优秀的框架,也可以在后面的评论中分享给我 ...
随机推荐
- python 相关操作
json转字典: #如:{"Message":"OK","RequestId":"9922A379-7373-492C-842A- ...
- Batch - %~dp0 modifiers
%~dp0 简易解释 The variable %0 in a batch script is set to the name of the executing batch file. The ~dp ...
- JAVA javah
{ 用法: javah [options] <classes>其中, [options] 包括: -o <file> 输出文件 (只能使用 - ...
- 【硬盘】RAID卡
独立磁盘冗余阵列,或简称磁盘阵列(Redundant Array of Independent Disks) RAID是一种把多块独立的物理硬盘按不同方式组合起来形成一个逻辑硬盘,一般分为硬RAID卡 ...
- bs4修改html文件和保存
一.需求 将2个html文件保存到本地浏览器,例如: A页面(我的博客主页) B页面(爬虫四大金刚) 然后将A页面中的爬虫链接,链接的a标签中的href属性修改成本地B页面的地址,实现在本地浏览A页面 ...
- NX二次开发-UFUN单对象选择对话框UF_UI_select_with_single_dialog
#include <uf.h> #include <uf_ui.h> ], void* user_data, UF_UI_selection_p_t select) { if ...
- csp-s模拟测试98
csp-s模拟测试98 $T1$??不是我吹我轻松手玩20*20.$T2$装鸭好像挺可做?$T3$性质数据挺多提示很明显? $One$ $Hour$ $Later$ 这$T1$什么傻逼题真$jb$难调 ...
- python入门 集合(四)
集合 集合是一个无序的不重复元素序列,可以迭代,也可以修改.集合迭代的时候元素是随机的. 集合通常用来 membership testing, 去重, 也可以用来求交集并集补集. 介绍一下如何创建集合 ...
- getmapping等无法解析
版本要改一下,4.1.6没有<dependency> <groupId>org.springframework</groupId> <artifactId&g ...
- Django中的HttpResponse和JsonResponse
Django中的HttpResponse和JsonResponse 我们在编写一些借口函数的时候,经常需要给调用者返回json格式的数据,那么如何返回可直接解析的数据呢? 首先第一种方式: from ...