zoukankan      html  css  js  c++  java
  • 再次复习python

    • python文件操作
      •   r  只读
      •        w 只写(同名直接覆盖)
      •        a 是w的升级版,实在同名文件中继续写,不会重写文件
      •       r + 可读可写    
    • 序列化:变量都存在内存中,如果将变量转换成可储存可传输的过程就叫序列化;
      • import json
        
        d = dict(name='fan', age=13)
        f = json.dumps(d)   #序列化为json对象
        print(type(f))   #<class 'str'>
        g = json.loads(f)   #反序列化为python识别的对象
        print(g)  #{'name': 'fan', 'age': 13}
        print(type(g))   #<class 'dict'>
        
        
        class Student(object):
            def __init__(self, name, age, score):
                self.name = name
                self.age = age
                self.score = score
        
        
        s = Student('fan', 28, 99)
        
        
        def myfun(stu):
            return {
                'name': stu.name,
                'age': stu.age,
                'score': stu.score
        
            }
        
        
        print(json.dumps(s, default=myfun))   #序列化python实例时,无法识别实例,可以通过指定函数转换成json可以识别的字典格式
    • 多进程:操作系统进行资源分配和调度的基本单位。
      •   
        from multiprocessing import Process
        
        import os
        
        
        def print_world(x):
            print('%s子进程来执行这个工作id:%s' % (x, os.getpid()))
            print('父进程id为%s' % os.getppid())
        
        
        if __name__ == '__main__':
            print('主进程id:%s' % os.getpid())
            p = Process(target=print_world, args=('test',))
            print('子进程开始运行')
            p.start()
            p.join()
            print('end son')
      • Pool进程池
        from multiprocessing import Pool
        import os,time, random
        
        
        def pri_sub(x):
            print('run task %s:%s'%(x, os.getpid()))
            start = time.time()
            time.sleep(random.random() * 3)
            end = time.time()
            print('task %s runs %0.2f seconds'%(x,(end - start)))
        
        
        if __name__ == '__main__':
            print('parend process %s'%os.getpid())
            p = Pool(4)
            for i in  range(5):
                p.apply_async(pri_sub,args=(i,))  #异步创建子进程
            print('等待子进程结束')
            p.close()
            p.join()
            print('所有的子进程结束')
      • subprocess模块可以让我们非常方便地启动一个子进程,然后控制其输入和输出
        import subprocess     #创建一个子进程去执行 命令  ipconfig
        
        subprocess.call('ipconfig')
      • 进程通信:Queue、Pipes
    • 多线程:线程是CPU调度的最小单位
  • 相关阅读:
    在Centos 7下编译openwrt+njit-client
    开博随笔
    Chapter 6. Statements
    Chapter 4. Arrays and Pointers
    Chapter 3. Library Types
    Chapter 2.  Variables and Basic Types
    关于stm32不常用的中断,如何添加, 比如timer10 timer11等
    keil 报错 expected an identifier
    案例分析 串口的地不要接到电源上 会烧掉
    案例分析 CAN OPEN 调试记录 进度
  • 原文地址:https://www.cnblogs.com/tarzen213/p/12288561.html
Copyright © 2011-2022 走看看