线程进程学习

简介: 笔记
# encoding: utf-8
"""
@author: lileilei
@site: 
@software: PyCharm
@file: login.py
@time: 2017/7/26 10:28
"""
import  requests,time
from  multiprocessing import  Pool
url='http://www.jd.com'
total=0
suc=0
fail=0
ecept=0
maxtime=0
mintime=0
gt3=0
lt3=0
def  baiduGent():
    global total
    global suc
    global  fail
    global  gt3
    global lt3
    global ecept
    try:
        st=time.time()
        conn=requests.get(url)
        res=conn.status_code
        if res==200:
            total+=1
            suc+=1
        else:
            total+=1
            fail+=1
        time_span = time.time() - st
        print(time_span)
        if time_span>3:
            gt3+=1
        else:
            lt3+=1
    except Exception as e:
        print(e)
        total+=1
        ecept+=1
if __name__ =='__main__':
    print('===========请求开始===========')
    start_time = time.time()
    pools = Pool(100)
    for i in range(10):
        pools.apply_async(baiduGent,args=())
    pools.close()
    pools.join()
from multiprocessing import Process,Queue 
import os, time, random
def write(q):
    print('Process to 产生: %s' % os.getpid())
    for value in ['苹果', '香蕉', '橘子']:
        print('产生 %s to queue...' % value)
        q.put(value)
        time.sleep(random.random())
def read(q):
    print('Process to 消费: %s' % os.getpid())
    while True:
        value = q.get()
        print('消费 %s from queue.' % value)
if __name__=='__main__':
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    pw.start()
    pr.start()
    pw.join()
    pr.terminate()
m=list(map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
print(m)
f=list(map(lambda x:True if x%3==0 else False,range(100)))
print(f)
import asyncio
@asyncio.coroutine
async def hello():
    print ('hello word')
    r=await asyncio.sleep(1)
    print('hello  again')
loop=asyncio.get_event_loop()
loop.run_until_complete(hello())
loop.close()
import threading
import asyncio
@asyncio.coroutine
def hello():
    print('Hello world! (%s)' % threading.currentThread())
    yield from asyncio.sleep(1)
    print('Hello again! (%s)' % threading.currentThread())
loop = asyncio.get_event_loop()
tasks = [hello(), hello()]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
import asyncio
@asyncio.coroutine
def wget(host):
    print('wegt %s ....'%host)
    connt=asyncio.open_connection(host,80)
    reder,writer=yield from connt
    hserd= 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % host
    writer.write(hserd.encode('utf-8'))
    yield from writer.drain()
    while 1:
        line=yield from reder.readline()
        if line ==b'\r\n':
            break
        print('%s header > %s' % (host, line.decode('utf-8').rstrip()))
        writer.close()
loop=asyncio.get_event_loop()
tasks=[wget(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
import asyncio
from aiohttp import web
async def index(request):
    await asyncio.sleep(0.5)
    return web.Response(body=b'<h1>Index</h1>',content_type='text/html')
async def hello(request):
    await asyncio.sleep(0.5)
    text = '<h1>hello, %s!</h1>' % request.match_info['name']
    return web.Response(body=text.encode('utf-8'),content_type='text/html')
async def init(loop):
    app = web.Application(loop=loop)
    app.router.add_route('GET', '/', index)
    app.router.add_route('GET', '/hello/{name}', hello)
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 8000)
    print('Server started at http://127.0.0.1:8000...')
    return srv
loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()
import asyncio,time
@asyncio.coroutine
async def hello():
    print ('hello word')
start_time=time.time()
loop=asyncio.get_event_loop()
tasks=[]
for i in range(10000):
    tasks.append(hello()) 
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
print('异步处理时间:%s'%(time.time()-start_time))
import time,gevent 
def print_s(num):
    st=time.time()
    conn=requests.get('http://www.jd.com')
    res=conn.status_code
    if res==200:
        print('chenggong')
    else:
        print('shibai')
start_time=time.time()
events=[gevent.spawn(print_s,num)for num in range(10000)]
gevent.joinall(events)
print('协程时间:%s'%(time.time()-start_time))
相关文章
|
11天前
|
UED 开发者 Python
探索操作系统的心脏:理解进程与线程
【8月更文挑战第31天】在数字世界的海洋中,操作系统犹如一艘巨轮,其稳定航行依赖于精密的进程与线程机制。本文将揭开这一机制的神秘面纱,通过深入浅出的语言和直观的代码示例,引领读者从理论到实践,体验进程与线程的魅力。我们将从基础概念出发,逐步深入到它们之间的联系与区别,最后探讨如何在编程实践中高效运用这些知识。无论你是初学者还是有经验的开发者,这篇文章都将为你的技术之旅增添新的航标。
|
8天前
|
监控 Java 调度
【Java学习】多线程&JUC万字超详解
本文详细介绍了多线程的概念和三种实现方式,还有一些常见的成员方法,CPU的调动方式,多线程的生命周期,还有线程安全问题,锁和死锁的概念,以及等待唤醒机制,阻塞队列,多线程的六种状态,线程池等
64 6
【Java学习】多线程&JUC万字超详解
|
4天前
|
存储 Java 数据处理
进程中的线程调度
进程是应用程序运行的基本单位,包括主线程、用户线程和守护线程。计算机由存储器和处理器协同操作,操作系统设计为分时和分任务模式。在个人PC普及后,基于用户的时间片异步任务操作系统确保了更好的体验和性能。线程作为进程的调度单元,通过覆写`Thread`类的`run`方法来处理任务数据,并由系统调度框架统一管理。微服务架构进一步将应用分解为多个子服务,在不同节点上执行,提高数据处理效率与容错性,特别是在大规模数据存储和处理中表现显著。例如,利用微服务框架可以优化算法,加速业务逻辑处理,并在不同区块间分配海量数据存储任务。
|
10天前
crash —— 输出属于同一个进程的所有线程
crash —— 输出属于同一个进程的所有线程
|
11天前
|
调度 开发者 Python
深入浅出操作系统:进程与线程的奥秘
【8月更文挑战第31天】 本文将带你探索操作系统中的核心概念——进程与线程。通过浅显易懂的语言和实际代码示例,我们将一起理解它们的定义、区别以及在操作系统中的作用。无论你是编程新手还是有一定经验的开发者,这篇文章都将为你打开一扇了解计算机内部工作原理的新窗口。
|
11天前
|
C# 开发者 数据处理
WPF开发者必备秘籍:深度解析数据网格最佳实践,轻松玩转数据展示与编辑大揭秘!
【8月更文挑战第31天】数据网格控件是WPF应用程序中展示和编辑数据的关键组件,提供排序、筛选等功能,显著提升用户体验。本文探讨WPF中数据网格的最佳实践,通过DevExpress DataGrid示例介绍其集成方法,包括添加引用、定义数据模型及XAML配置。通过遵循数据绑定、性能优化、自定义列等最佳实践,可大幅提升数据处理效率和用户体验。
27 0
|
11天前
|
消息中间件 Unix Linux
深入浅出操作系统:进程与线程的奥秘
【8月更文挑战第31天】本文将带你一探操作系统中最为神秘的两个概念——进程和线程。我们将从基础的定义出发,逐步深入到它们在操作系统中的实现原理,并通过代码示例揭示它们在实际编程中的应用。无论你是初学者还是有一定经验的开发者,这篇文章都将为你提供新的视角和理解。
|
11天前
|
开发者 Python
深入浅出操作系统:进程与线程的奥秘
【8月更文挑战第31天】在数字世界的幕后,操作系统扮演着至关重要的角色。本文将揭开进程与线程这两个核心概念的神秘面纱,通过生动的比喻和实际代码示例,带领读者理解它们的定义、区别以及如何在编程中运用这些知识来优化软件的性能。无论你是初学者还是有一定经验的开发者,这篇文章都将为你提供新的视角和实用技巧。
|
13天前
|
Linux 调度 开发者
探索操作系统核心:进程与线程的管理
【8月更文挑战第30天】在数字世界的心脏,操作系统扮演着至关重要的角色。它不仅是计算机硬件与软件之间的桥梁,更是管理和调度计算资源的核心。本文将深入探讨操作系统中最为关键的两个概念:进程与线程。我们将从基本的定义出发,逐步揭示它们之间的区别、联系以及如何在操作系统中高效管理这些基础单位。通过实际代码示例,我们将进一步理解操作系统如何精确控制和优化进程与线程的运行,确保系统的稳定与高效。无论你是软件开发者还是系统管理员,这篇文章都将为你打开一扇了解操作系统深层工作机制的大门。
|
13天前
|
UED Python
深入理解操作系统:进程与线程的管理
【8月更文挑战第29天】本文将通过浅显的语言和生动的比喻,带你走进操作系统的核心世界,探索进程与线程的秘密。我们将从基础概念出发,逐步深入到它们在操作系统中的管理方式,以及如何影响计算机的性能和稳定性。文章旨在启发读者思考操作系统设计的哲学,同时提供实用的知识,帮助理解现代计算机系统的运作原理。

相关实验场景

更多