python进程,线程的一点小总结1

上一篇 / 下一篇  2015-11-16 16:37:54 / 个人分类:python

    我是去看大拿的脚本开始学习关于python进程,线程这些东西的,很多是零散的知识,满足当前可用,没有仔细去归纳,学到的时候再说吧:
    有这样的需求,就是我们用python去写一个程序,做什么呢?读取dropbox中的某个文件,比如:adb shell  dumpsys dropbox --print 2015-04-08,这种,如何做?使用subprocess去开新的线程,获取我们的输入,输出,错误信息等等

p=subprocess.Popen("G:\\adt-bundle-windows-x86_64-20140321\\sdk\\platform-tools\\adb.exe shell dumpsys dropbox --print 2015-10-08 10:57:57 SYSTEM_LAST_KMSG", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)注意是双斜杠

subprocess.Popen()开新的线程,把要在cmd中执行的东西丢进第一个字符串里面,后面的内容参照本例就ok,这句话一输入,其实后台就已经在连接上的手机执行这条命令了:
xx=p.stdout.read() 这句话把进程里面的管道流读出来,
import subprocess
p=subprocess.Popen("G:\\adt-bundle-windows-x86_64-20140321\\sdk\\platform-tools\\adb.exe shell dumpsys 

dropbox --print 2015-10-08 10:57:57 SYSTEM_LAST_KMSG", shell=True, stdout=subprocess.PIPE, 

stderr=subprocess.STDOUT)
xx=p.stdout.read()
f=open('E:m.txt','w')
f.write(xx)
f.close()

这个例子就是简单的通过管道去打开一个cmd命令输入并执行里面的adb命令,之后把得到的输出读出来写到f盘的txt文

件中去

把10个app的包名放一个列表里,然后通过这个管道命令,依次执行这10个包的cpu,

meminfo,并且依次输入到10个对应的txt文件里
  
pkglist=['com.by.fishgame','com.joym.armorhero','com.xbl.staract2']    #包名扔进list
for i in pkglist:
p=subprocess.Popen("G:\\adt-bundle-windows-x86_64-20140321\\sdk\\platform-tools\\adb.exe shell 

dumpsys meminfo %s" %(i), shell=True, 
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)   #弄个循环依次从

list里面取一个个的包名开跑adb命令
xx=p.stdout.read()
f=open('E:\\%s.txt'%(i),'w')  #注意%S的使用真鸡毛各种方便,就是把取出的包名拿出来给txt文件命名,

把各自的adb跑的信息传进各自的txt文件中
f.write(xx)
f.close()

这句开跑monkey
p=subprocess.Popen("G:\\adt-bundle-windows-x86_64-20140321\\sdk\\platform-tools\\adb.exe shell monkey -p com.by.fishgame --pct-touch 65 --pct-motion 30 --ignore-crashes --ignore-timeouts --ignore-security-exceptions --monitor-native-crashes --throttle 200 -v -v 5000", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)




TAG: Python python

 

评分:0

我来说两句

Open Toolbar