Skip to content

Commit c11ea72

Browse files
author
luyishisi
committed
python多进程
1 parent 99237bc commit c11ea72

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

python_aiml_test/start/brain-2.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
# kernel.learn("std-startup.xml")
1313
# kernel.respond("load aiml pattern")
1414
print("begin game")
15-
a = "http://tsn.baidu.com/text2audio?tex=hello, you can talk to me! just tell me&lan=zh&per=0&pit=5&spd=2&cuid=7519663&ctp=1&tok=24.91b892cbba2c73d07f9fba69182b7960.2592000.1456136364.282335-7519663&qq-pf-to=pcqq.c2c"
16-
subprocess.call(["mpg123",a])
15+
subprocess.PIPE
16+
a = "http://tsn.baidu.com/text2audio?tex=hello, you can talk to me! just tell me&lan=zh&per=0&pit=5&spd=4&cuid=7519663&ctp=1&tok=24.91b892cbba2c73d07f9fba69182b7960.2592000.1456136364.282335-7519663&qq-pf-to=pcqq.c2c"
17+
p = subprocess.Popen(["mpg123",a])
18+
print (p.pid)
19+
p.wait()
1720
rebot = "rebot say: hello, you can talk to me! Just tell me "
1821
print (rebot)
1922
while True:
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#coding:utf-8
22
import subprocess
3-
subprocess.call(["mpg123","http://tsn.baidu.com/text2audio?tex=hello my name is luyi&lan=zh&per=0&pit=5&spd=2&cuid=7519663&ctp=1&tok=24.91b892cbba2c73d07f9fba69182b7960.2592000.1456136364.282335-7519663&qq-pf-to=pcqq.c2c"])
4-
#subprocess.call(["mpg123","http://music.baidutt.com/up/kwcawskw/dsskw.mp3"])
3+
#p = subprocess.popen(["mpg123","http://tsn.baidu.com/text2audio?tex=hello my name is luyi&lan=zh&per=0&pit=5&spd=2&cuid=7519663&ctp=1&tok=24.91b892cbba2c73d07f9fba69182b7960.2592000.1456136364.282335-7519663&qq-pf-to=pcqq.c2c"])
4+
p = subprocess.Popen('ls',shell=True,stdout=subprocess.PIPE)
5+
stdoutput,erroutput = p.communicate('/home/zoer')
6+
7+
print stdoutput[0]
8+
print erroutput
9+

python_multithreading/ping_LAN.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin python
2+
3+
from threading import Thread
4+
import subprocess
5+
from Queue import Queue
6+
import datetime
7+
8+
num_threads = 100
9+
ips = ['192.168.0.1','192.168.255.255']
10+
for i in range(25):
11+
for j in range(255):
12+
l = '192.168.'+str(i)+'.'+str(j)
13+
ips.append(l)
14+
#print (ips)
15+
#print(ips)
16+
q = Queue()
17+
def pingme(i,queue):
18+
while True:
19+
ip = queue.get()
20+
#print('thread %s pinging %s' %(i,ip))
21+
begin = datetime.datetime.now()
22+
ret = subprocess.call('ping -c 1 %s' % ip,shell=True,stdout=open('/dev/null','w'),stderr = subprocess.STDOUT)
23+
#ret.wait()
24+
end = datetime.datetime.now()
25+
diff = end-begin
26+
27+
#if diff.seconds < 10:
28+
# print ('%s time %s' %(ip,diff.seconds))
29+
30+
if ret == 0:
31+
print ('%s is alive!' %ip)
32+
elif ret == 1:
33+
pass
34+
#print ('%s is down...' %ip)
35+
queue.task_done()
36+
37+
for i in range(num_threads):
38+
t=Thread(target=pingme,args=(i,q))
39+
t.setDaemon(True)
40+
t.start()
41+
#print (i)
42+
43+
for ip in ips:
44+
q.put(ip)
45+
#print (ip)
46+
47+
print ('main thread waiting....')
48+
q.join()
49+
print ('done')
50+

0 commit comments

Comments
 (0)