2019年6月16日
摘要: # -*- coding: utf-8 -*- import time def consumer(item): time.sleep(0.01) pass def producer(target, seq): for item in seq: target(item) start_time = ti 阅读全文
posted @ 2019-06-16 22:41 lilyxiaoyy 阅读(228) 评论(0) 推荐(0) 编辑
摘要: # -*- coding: utf-8 -*- import queue if __name__ == '__main__': '''先进先出''' que = queue.Queue(3) que.put("first") que.put("second") que.put(["a", "b", 阅读全文
posted @ 2019-06-16 19:59 lilyxiaoyy 阅读(1127) 评论(0) 推荐(0) 编辑
摘要: # -*- coding: utf-8 -*- from threading import Timer def talk(name): print("%s is talking." % name) if __name__ == '__main__': '''Timer(等待多少秒, 执行的函数, a 阅读全文
posted @ 2019-06-16 19:45 lilyxiaoyy 阅读(4145) 评论(0) 推荐(0) 编辑
摘要: # -*- coding: utf-8 -*- import time import threading from threading import Thread, Event def conn_mysql(): '''连接数据库''' print("(%s) start to conn_mysql 阅读全文
posted @ 2019-06-16 19:40 lilyxiaoyy 阅读(1107) 评论(0) 推荐(0) 编辑
摘要: # -*- coding: utf-8 -*- import time from threading import Thread, Semaphore def go_ktv(i): print("user%s正在....ktv." % (i)) time.sleep(2) if __name__ = 阅读全文
posted @ 2019-06-16 18:05 lilyxiaoyy 阅读(1204) 评论(0) 推荐(0) 编辑
摘要: GIL锁(Global Interpreter Lock)全局解释器锁 在Cpython解释器中,同一进程下开启的多线程,同一时刻只能有一个线程执行,无法利用多核优势.那么,我们改如何解决GIL锁的问题呢? 1.更换cpython为jpython(不建议) 2.使用多进程完成多线程的任务 3.在使用 阅读全文
posted @ 2019-06-16 16:39 lilyxiaoyy 阅读(349) 评论(0) 推荐(0) 编辑
  2019年6月15日
摘要: # -*- coding: utf-8 -*- import time from multiprocessing import Process from threading import Thread def func(): ret = 0 for i in range(10000000): ret 阅读全文
posted @ 2019-06-15 00:13 lilyxiaoyy 阅读(789) 评论(0) 推荐(0) 编辑
  2019年6月14日
摘要: # -*- coding: utf-8 -*- from threading import Thread talk_l = [] format_l = [] def talk(): '''用户输入''' while 1: inp = input(">>: ").strip() if not inp: 阅读全文
posted @ 2019-06-14 22:40 lilyxiaoyy 阅读(218) 评论(0) 推荐(0) 编辑
摘要: 线程比进程的开销小 import time from multiprocessing import Process from threading import Thread def func(name): print(f"我是{name}") if __name__ == '__main__': s 阅读全文
posted @ 2019-06-14 19:52 lilyxiaoyy 阅读(563) 评论(0) 推荐(0) 编辑
摘要: 并行 多个任务同时执行,建立在多核CPU基础上。(4个人吃饭,有4套餐具,可以同时吃) 并发 经过CPU的调度,快速的在单核CPU上多个任务的快速切换。(4个人吃饭,只有一套餐具,只能一个人一个人的吃) 进程 (公司) 资源单位,进程之间是相互独立的,进程自己是不能运行的,进程中的线程是负责执行和调 阅读全文
posted @ 2019-06-14 19:43 lilyxiaoyy 阅读(622) 评论(0) 推荐(0) 编辑
摘要: '''操作系统的作用: 1、把硬件丑陋复杂的接口隐藏起来,为应用程序提供良好接口 2、管理,调用进程,并且把进程之间对硬件的竞争变得有序化多道技术: 1、产生背景:为了实现单cpu下的并发效果 2、为了两部分: 1:空间上的复用(必须实现硬件层面的隔离) 2:时间上的复用(复用cpu的时间片) 什么 阅读全文
posted @ 2019-06-14 14:25 lilyxiaoyy 阅读(229) 评论(0) 推荐(0) 编辑
摘要: ''' 1、互联网协议osi七层模型? 应用层 表示层 会话层 传输层 网络层 数据链路层 物理层 2、arp协议属于七层模型中的那一层,并简述协议内容? arp协议属于网络层 ARP : Address Resolution Protocol即地址解析协议,实现通过IP地址得知其物理地址。 主机发 阅读全文
posted @ 2019-06-14 12:08 lilyxiaoyy 阅读(194) 评论(0) 推荐(0) 编辑
  2019年6月13日
摘要: try: dic = {'a': 1,} dic['b'] # KeyError lst = ['a', 'b'] lst[10] # IndexError s = "2GB" s = int(s) # ValueError except ValueError as e: # e为异常的值 prin 阅读全文
posted @ 2019-06-13 10:29 lilyxiaoyy 阅读(208) 评论(0) 推荐(0) 编辑
  2019年6月10日
摘要: f = shelve.open(filename) # 通过open方法打开一个文件,并拿到文件对象f f['a'] = "Python's obj" # 以字典的方式将Python中的任意对象持久化 a = f['a'] a = 'new str' f['a'] = a f.close() # 最 阅读全文
posted @ 2019-06-10 11:36 lilyxiaoyy 阅读(142) 评论(0) 推荐(0) 编辑
  2019年6月6日
摘要: # coding:gbk import requests response = requests.get('http://www.sina.com.cn/') print(response) print(response.status_code) # 200正常,404找不到网页,503等5开头的是 阅读全文
posted @ 2019-06-06 23:44 lilyxiaoyy 阅读(291) 评论(0) 推荐(0) 编辑
摘要: # -*- coding: utf-8 -*- import os import time from multiprocessing import Pool def func(n): print("%s子进程(%s)" % (n, os.getpid())) time.sleep(0.5) retu 阅读全文
posted @ 2019-06-06 23:42 lilyxiaoyy 阅读(496) 评论(0) 推荐(0) 编辑
摘要: Pool内的进程数默认是cpu核数,假设为4(查看方法os.cpu_count())开启6个客户端,会发现2个客户端处于等待状态在每个进程内查看pid,会发现pid使用为4个,即多个客户端公用4个进程tcp_server.py # -*- coding: utf-8 -*- import os fr 阅读全文
posted @ 2019-06-06 19:38 lilyxiaoyy 阅读(543) 评论(0) 推荐(0) 编辑
摘要: # coding:utf-8 import os import time from multiprocessing import Pool def func(n): print("%s:%s" % (os.getpid(), n)) return n**2 def func2(n): print(" 阅读全文
posted @ 2019-06-06 19:06 lilyxiaoyy 阅读(2194) 评论(0) 推荐(0) 编辑
摘要: # coding:utf-8 import os import time from multiprocessing import Pool def func(n): print("%s:%s" % (os.getpid(), n)) return n**2 def func2(n): print(" 阅读全文
posted @ 2019-06-06 19:05 lilyxiaoyy 阅读(517) 评论(0) 推荐(0) 编辑
摘要: 进程池的概念,定义一个池子,在里面放上固定数量的进程,有需求来了,就拿一个池中的进程来处理任务,等到处理完毕,进程并不关闭,而是将进程再放回进程池中继续等待任务。如果有很多任务需要执行,池中的进程数量不够,任务就要等待之前的进程执行任务完毕归来,拿到空闲进程才能继续执行。也就是说,池中进程的数量是固 阅读全文
posted @ 2019-06-06 18:08 lilyxiaoyy 阅读(3824) 评论(0) 推荐(0) 编辑
摘要: python线程的事件用于主线程控制其他线程的执行,事件主要提供了三个方法 set、wait、clear。 事件处理的机制:全局定义了一个“Flag”,如果“Flag”值为 False,那么当程序执行 event.wait 方法时就会阻塞,如果“Flag”值为True,那么event.wait 方法 阅读全文
posted @ 2019-06-06 16:56 lilyxiaoyy 阅读(1411) 评论(0) 推荐(0) 编辑
摘要: 提前设定好,一个房间只有4个床(计数器现在为4),那么同时只能四个人进来,谁先来的谁先占一个床(acquire,计数器减1),4个床满了之后(计数器为0了),第五个人就要等着,等其中一个人出来(release,计数器加1),他就去占用那个床了。 互斥锁同时只允许一个线程更改数据,而信号量Semaph 阅读全文
posted @ 2019-06-06 16:14 lilyxiaoyy 阅读(1614) 评论(0) 推荐(0) 编辑
摘要: 进程间数据是独立的,可以借助于队列或管道实现通信,二者都是基于消息传递的 虽然进程间数据独立,但可以通过Manager实现数据共享,但是,为了确保数据的安全性,需要通过加锁Lock来确保数据的安全性,如抢票. import os from multiprocessing import Process 阅读全文
posted @ 2019-06-06 15:52 lilyxiaoyy 阅读(1023) 评论(0) 推荐(0) 编辑
摘要: 进程间通信(IPC)方式二:管道(不推荐使用,了解即可),会导致数据不安全的情况出现,后面我们会说到为什么会带来数据 不安全的问题。 # coding:utf-8 from multiprocessing import Process, Pipe def func(conn2): conn2.sen 阅读全文
posted @ 2019-06-06 15:49 lilyxiaoyy 阅读(3745) 评论(0) 推荐(0) 编辑
  2019年6月4日
摘要: 队列的简单使用,队列先进先出 import queue # 不能用于多进程之间的通讯,可以用于多线程间的通讯 from multiprocessing import Queue # 可以用于进程之间的数据共享 q = Queue(3) # 创建一个队列对象,队列长度为3 q.put(1) q.put 阅读全文
posted @ 2019-06-04 15:08 lilyxiaoyy 阅读(1067) 评论(0) 推荐(0) 编辑
  2019年6月3日
摘要: 队列的简单使用,队列先进先出 import queue # 不能用于多进程之间的通讯,可以用于多线程间的通讯 from multiprocessing import Queue # 可以用于进程之间的数据共享 q = Queue(3) # 创建一个队列对象,队列长度为3 q.put(1) q.put 阅读全文
posted @ 2019-06-03 23:33 lilyxiaoyy 阅读(5684) 评论(0) 推荐(0) 编辑
摘要: # coding:utf-8 import time import json from multiprocessing import Process, Lock def func(n, loc): dic = json.load(open('db.py')) print("用户[%s]你好!目前剩余 阅读全文
posted @ 2019-06-03 22:37 lilyxiaoyy 阅读(810) 评论(0) 推荐(0) 编辑
摘要: 什么是守护进程? import time from multiprocessing import Process def func(name): while 1: time.sleep(0.5) print(f"我是{name}") if __name__ == '__main__': p1 = P 阅读全文
posted @ 2019-06-03 14:09 lilyxiaoyy 阅读(3104) 评论(0) 推荐(0) 编辑
摘要: # coding:utf-8 from multiprocessing import Process import time, os def run(): print('子', os.getpid()) if __name__ == '__main__': p = Process(target=ru 阅读全文
posted @ 2019-06-03 13:59 lilyxiaoyy 阅读(1542) 评论(0) 推荐(0) 编辑
摘要: # coding:utf-8 import os from multiprocessing import Process class MyProcess(Process): def __init__(self, nam): super().__init__() self.nam = nam # 因为 阅读全文
posted @ 2019-06-03 13:19 lilyxiaoyy 阅读(1746) 评论(0) 推荐(0) 编辑
摘要: terminate 关闭进程,不会立即关闭,有个等着操作系统去关闭这个进程的时间,所以is_alive立刻查看的结果可能还是存活,但是稍微等一会,就被关掉了 # coding:utf-8 import time from multiprocessing import Process class My 阅读全文
posted @ 2019-06-03 13:03 lilyxiaoyy 阅读(3388) 评论(0) 推荐(0) 编辑
摘要: tcp_server.py # coding:utf-8 from socket import * from multiprocessing import Process def talk(conn, addr): print("子进程开始.") while 1: try: client_from_ 阅读全文
posted @ 2019-06-03 12:46 lilyxiaoyy 阅读(2646) 评论(0) 推荐(0) 编辑
摘要: 进程之间数据是隔离的,相当于两家公司之间是隔离的 from multiprocessing import Process def func(lst, name): lst.append(66) print(name, lst) if __name__ == '__main__': lst = [1, 阅读全文
posted @ 2019-06-03 11:41 lilyxiaoyy 阅读(380) 评论(0) 推荐(0) 编辑
  2019年6月2日
摘要: # coding:utf-8 import os import re from multiprocessing import Process def func(file, mode): with open(file, mode, encoding="utf-8") as f: f.write("子进 阅读全文
posted @ 2019-06-02 23:01 lilyxiaoyy 阅读(5714) 评论(0) 推荐(0) 编辑
摘要: # coding:utf-8 import os from multiprocessing import Process def func(n): print("子进程号: %s 参数: %s" % (os.getpid(), n)) if __name__ == '__main__': print 阅读全文
posted @ 2019-06-02 22:01 lilyxiaoyy 阅读(1826) 评论(0) 推荐(0) 编辑
摘要: # coding:utf-8 import os import time from multiprocessing import Process def func(): print("func start...") time.sleep(1) print("func end...") print(" 阅读全文
posted @ 2019-06-02 21:46 lilyxiaoyy 阅读(1339) 评论(0) 推荐(0) 编辑
摘要: # coding:utf-8 import os import time from multiprocessing import Process def func1(): print("5func1 start...") print("6子进程1的进程号>:", os.getpid()) print 阅读全文
posted @ 2019-06-02 20:09 lilyxiaoyy 阅读(581) 评论(0) 推荐(0) 编辑
摘要: 上次内容回顾## 网络编程```socketosi七层协议tcp udp 传输层的两个常用协议tcp:面向连接的,可靠的,消息格式是面向流的udp:面向无连接的,不可靠的,消息格式是面向包的```#### tcp和udp协议的socket``` #服务端 import socket server = 阅读全文
posted @ 2019-06-02 19:50 lilyxiaoyy 阅读(224) 评论(0) 推荐(0) 编辑
  2019年5月30日
摘要: hashlib和hmac都可以进行加盐的md5加密,即使是相同的盐和数据,加密出来的结果是不一样的哦! 加密的内容必须是字节! import hmac import hashlib print(hashlib.md5(b"hello world").hexdigest()) print(hmac.n 阅读全文
posted @ 2019-05-30 13:16 lilyxiaoyy 阅读(733) 评论(0) 推荐(0) 编辑
  2019年5月29日
摘要: 首先看下sys模块里的标准输出stdout import sys sys.stdout.write("111") sys.stdout.write("222") 执行结果: 111222 看下带有回车\r的情况 import sys sys.stdout.write("111") sys.stdou 阅读全文
posted @ 2019-05-29 23:56 lilyxiaoyy 阅读(743) 评论(0) 推荐(0) 编辑

返回
顶部