12 2019 档案
摘要:#_author:来童星#date:2019/12/31import socketdef main(): sk=socket.socket(socket.AF_INET,socket.SOCK_STREAM) address=('localhost',8089) sk.bind(address) s
阅读全文
摘要:#server端 #_author:来童星#date:2019/12/29#利用io多路复用实现并发聊天import socketimport selectsk=socket.socket()address=('127.0.0.1',8800)sk.bind(address)sk.listen(3)
阅读全文
摘要:一。四种io阻塞1.io阻塞:(1 等待数据处于阻塞状态(2从内核copy到用户态处于阻塞状态2.非io阻塞只有从内核copy到用户态处于阻塞状态3.io多路复用 》优势:可以同时监听多个对象(1从check 》ready 通过selec函数来做,处于阻塞状态(2从内核copy到用户态处于阻塞状态3
阅读全文
摘要:1 IO模型前戏准备 在进行解释之前,首先要说明几个概念:用户空间和内核空间进程切换进程的阻塞文件描述符缓存 I/O 用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2的32次方)。 操作系统的核心是内核,独立于普通的应用程序,可以访
阅读全文
摘要:#_author:来童星#date:2019/12/24# Scrapy爬虫框架的使用#1.安装Twisted模块 https://www.lfd.uci.edu/~gohlke/pythonlibs/#2.单击Twisted索引import scrapyfrom scrapy.crawler im
阅读全文
摘要:#_author:来童星#date:2019/12/22 import pygame import sys pygame.init() size=width,height=640,480 screen=pygame.display.set_mode(size) color=(0,0,0) ball=
阅读全文
摘要:#_author:来童星#date:2019/12/21# 事件处理import wxclass MyFrame(wx.Frame): def __init__(self,parent,id): wx.Frame.__init__(self,parent,id,'用户登录',size=(400,30
阅读全文
摘要:#_author:来童星#date:2019/12/20#创建一个wx.App的子类import wxclass App(wx.App): #初始化方法 def OnInit(self): frame=wx.Frame(parent=None,title='hello wxpython')# 创建窗
阅读全文
摘要:#_author:来童星#date:2019/12/19import pymysql#1.打开数据库连接db=pymysql.connect('localhost','root','root','test')#2.使用cursor()方法创建一个游标对象cursor=db.cursor()#3.使用
阅读全文
摘要:#_author:来童星#date:2019/12/18def division(): num1=int(input('请输入被除数:')) num2=int(input('请输入除数:')) if num2==0: raise ValueError('除数不能为0') result=num1//n
阅读全文
摘要:#_author:来童星#date:2019/12/18def division(): num1=int(input('请输入被除数:')) num2=int(input('请输入除数:')) result=num1//num2 print(result)if __name__=='__main__
阅读全文
摘要:import sockethost='127.0.0.1'port=8080web=socket.socket()web.bind((host,port))web.listen(5)# 设置最多连接数print('服务器等待客户端连接')#开启死循环while True: coon,addr=web
阅读全文
摘要:#_author:来童星#date:2019/12/17#生产者消费者模式-->线程from queue import Queueimport random,time,threading#生产者class Producer(threading.Thread): def __init__(self,n
阅读全文
摘要:#_author:来童星#date:2019/12/17#互斥锁from threading import Thread,Lockimport timen=100def func(): global n mutex.acquire() temp=n time.sleep(0.1) n=temp-1
阅读全文
摘要:#_author:来童星#date:2019/12/17from threading import Threadimport timedef plus(): print('子线程1开始') global num num+=50 print('num is %d'%num) print('子线程1结束
阅读全文
摘要:#_author:来童星#date:2019/12/17#使用Thread创建线程import threadingimport timeclass Sunthread(threading.Thread): def run(self): for i in range(3): time.sleep(1)
阅读全文
摘要:#_author:来童星#date:2019/12/17#使用threading模块创建线程import threading,timedef process(): for i in range(3): time.sleep(1) print('thread name is %s'%threading
阅读全文
摘要:#_author:来童星#date:2019/12/17#使用队列在进程间通信from multiprocessing import Process,Queueimport time#向队列中写入数据def write_task(q):# 一定要将q传进去 if not q.full(): for
阅读全文
摘要:#_author:来童星#date:2019/12/17#多进程队列的使用from multiprocessing import Queueif __name__=='__main__': q=Queue(3)# 初始化一个Queue对象,最多可接收3条put消息 q.put('消息1') q.pu
阅读全文
摘要:#_author:来童星#date:2019/12/17#通过队列实现进程间通信from multiprocessing import Processdef plus(): print(' 子进程1开始 ') global g_num g_num+=50 print('g_num is %d'%g_
阅读全文
摘要:#_author:来童星#date:2019/12/17#通过队列实现进程间的通信from multiprocessing import Poolimport osimport timedef func(name): print('子进程(%s)执行func %s...'%(os.getpid(),
阅读全文
摘要:#_author:来童星#date:2019/12/17#通过队列实现进程间的通信from multiprocessing import Poolimport osimport timedef func(name): print('子进程(%s)执行func %s...'%(os.getpid(),
阅读全文
摘要:#_author:来童星#date:2019/12/17from multiprocessing import Processimport timeimport os#两个子进程将会调用的两个方法def child_1(i): print('子进程(%s)开始执行,父进程为(%s)'%(os.get
阅读全文
摘要:#_author:来童星#date:2019/12/17# 使用Process子类创建进程from multiprocessing import Processimport timeimport os#继承Process类class SubProcess(Process): #由于Process类本
阅读全文
摘要:#_author:Ltx#date:2019/12/16import timeimport randomclass account: ''' 1.存款功能 2.取款功能 3.打印交易详情 3.1--每次交易的时间 3.2--存款或者取款的金额 3.3每次交易后的金额 ''' def __init__
阅读全文
摘要:#_author:来童星#date:2019/12/15import os#1# print(os.name)# nt >windows操作系统#2 用于获取当前操作系统的换行符# print(os.linesep)# 为什么不显示# # 用于获取当前操作系统所使用的路径分隔符# print(os.
阅读全文
摘要:#_author:来童星#date:2019/12/13from datetime import datetimeclass medicine: name='' price=0 PD='' Exp='' def __init__(self,name,price,PD,Exp): self.name=
阅读全文
摘要:#_author:来童星#date:2019/12/12import geventimport timedef func1(): print('\033[31;1mfun1 starting...\033[0m',time.ctime()) gevent.sleep(2) print('\033[3
阅读全文
摘要:#_author:来童星#date:2019/12/12from greenlet import greenletdef test1(): print(12) gr2.switch() print(34) gr2.switch()def test2(): print(56) gr1.switch()
阅读全文
摘要:#_author:来童星#date:2019/12/12def consumer(name): print(" >start...") while True: new_baozi = yield print("[%s] is eating baozi %s" % (name, new_baozi))
阅读全文
摘要:#_author:来童星#date:2019/12/11#1.拼接字符串mon_en='Remerbrance is a form of meeting,Frgetfulness is a form of freedom 'mon_cn='记忆是一种相遇,遗忘是一种自由'print(mon_en+'
阅读全文
摘要:#_author:来童星#date:2019/12/11#Pipefrom multiprocessing import Process, Pipedef f(conn): conn.send([42, None, 'hello']) conn.close()if __name__ == '__ma
阅读全文
摘要:#_author:来童星#date:2019/12/11#Managersfrom multiprocessing import Process, Managerdef f(d, l,n): d[n] = '1' d['2'] = 2 d[0.25] = None l.append(n) print
阅读全文
摘要:#_author:来童星#date:2019/12/11# 进程间通讯 # 不同进程间内存是不共享的,要想实现两个进程间的数据交换,可以用以下方法:# Queues# 使用方法跟threading里的queue类似:from multiprocessing import Process, Queue
阅读全文
摘要:#_author:来童星#date:2019/12/10#1.year=[89,98,00,75,68,37,58,90]for index,value in enumerate(year): if str(value)!='0': year[index]=int('19'+str(value))
阅读全文
摘要:py2编码 tr和unicode str和unicode都是basestring的子类。严格意义上说,str其实是字节串,它是unicode经过编码后的字节组成的序列。对UTF-8编码的str'苑'使用len()函数时,结果是3,因为utf8编码的'苑' == '\xe8\x8b\x91'。 而un
阅读全文
摘要:#_author:来童星#date:2019/12/9import jsons='star'a=s.encode('utf8')print(s,type(s))# star <class 'str'>print(a.decode('utf8'))# stars1='星星' # unicode类型,一
阅读全文
摘要:#coding:utf8#一#1.在python2中,默认以ASCII编码chcp 936import sysprint sys.getdefaultencoding()# ascii#str:bytess1='来星hello' #存的是字节,数据类型是str(bytes就是str)# print
阅读全文
摘要:#_author:来童星#date:2019/12/8#1.回顾class school: def __init__(self,name): self.name=name def __str__(self): return self.names1=school('shanghai')s2=schoo
阅读全文
摘要:1.通过代理IP的好处: (1)隐藏自己真实上网地址 (2)突破宽带商访问限制
阅读全文
摘要:#_author:来童星#date:2019/12/6#1.获取当前日期import datetimeprint(datetime.date.today())# 2019-12-06#2.使用today和now获取当前日期和时间,时间精确到毫秒级print(datetime.datetime.tod
阅读全文
摘要:创建一个“队列”对象import Queueq = Queue.Queue(maxsize = 10)Queue.Queue类即是一个队列的同步实现。队列长度可为无限或者有限。可通过Queue的构造函数的可选参数maxsize来设定队列长度。如果maxsize小于1就表示队列长度无限。 将一个值放入
阅读全文
摘要:------------------------------------------------------------------------------------------------------------------------
阅读全文
摘要:条件同步和条件变量同步差不多意思,只是少了锁功能,因为条件同步设计于不访问共享资源的条件环境。event=threading.Event():条件环境对象,初始值 为False event.isSet():返回event的状态值;event.wait():如果 event.isSet()==Fals
阅读全文
摘要:有一类线程需要满足条件之后才能够继续执行,Python提供了threading.Condition 对象用于条件变量线程的支持,它除了能提供RLock()或Lock()的方法外,还提供了 wait()、notify()、notifyAll()方法。 lock_con=threading.Condit
阅读全文
摘要:信号量(也是一把锁)用来控制线程并发数的,BoundedSemaphore或Semaphore管理一个内置的计数 器,每当调用acquire()时-1,调用release()时+1。 计数器不能小于0,当计数器为 0时,acquire()将阻塞线程至同步锁定状态,直到其他线程调用release()。
阅读全文
摘要:#_author:来童星#date:2019/12/2# Python的线程在GIL的控制之下,线程之间,对整个python解释器,对python提供的CAPI的访问都是互斥的,# 这可以看作是Python内核级的互斥机制。但是这种互斥是我们不能控制的,我们还需要另外一种可控的互斥机制———用户级互
阅读全文
摘要:锁机制:为了防止cpu切换速度过快,造成我们不想要的结果,将代码用锁锁住,执行完之后在释放 acquire() >上锁 release() >解锁
阅读全文
摘要:1.t.setDaemon(True) 2.threading.current_thread() 3.threading.active_count()
阅读全文

浙公网安备 33010602011771号