目標:
1.os.fork簡單示例
3.使用os.fork多程序解決tcpserver多客戶端連線問題
1.os.fork簡單示例
**如下:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import
ospid =os.fork()
pidprint
'--------------------'if
pid:
print pid,'
from parent
'else
:
print pid,'
from child
'
**如下:
#!/usr/bin/env python
#coding:utf8
import
osimport
subprocess
import
sysips = ('
192.168.80.%s
' % i for i in xrange(1, 255)) #
生成器表示式
for ip in
ips:
pid =os.fork()
ifnot
pid:
return_val = subprocess.call('
ping -c1 %s &> /dev/null
' %ip, shell=true)
if return_val ==0:
"%s:up
" %ip
else
:
"%s:down
" %ip
sys.exit(0)
3.使用os.fork多程序解決tcpserver多客戶端連線問題
**如下:
#!/usr/bin/env python
#coding:utf8
import
osimport
time
from socket import
socket, af_inet, sock_stream, sol_socket, so_reuseaddr
import
syshost = ''
port = 12345addr =(host, port)
s =socket(af_inet, sock_stream)
s.setsockopt(sol_socket, so_reuseaddr, 1)
s.bind(addr)
s.listen(1)
while
true:
cli_sock, cli_addr =s.accept()
pid =os.fork()
ifpid:
while
true:
result = os.waitpid(-1, os.wnohang)
if result[0] ==0:
break
cli_sock.close()
else
: s.close()
while
true:
data = cli_sock.recv(1024).strip()
ifnot
data:
break
cli_sock.send(
'[%s] %s\r\n
' %(time.ctime(), data))
cli_sock.close()
sys.exit(0)
s.close()
**如下:
#!/usr/bin/env python
#coding:utf8
import
subprocess
import
threading
defping(host):
result =subprocess.call(
'ping -c2 %s &> /dev/null
' % host, shell=true
)if result ==0:
"%s:up
" %host
else
:
"%s:down
" %host
if__name__ == '
__main__':
ips = ('
172.40.55.%s
' % i for i in xrange(1, 255))
for ip in
ips:
t = threading.thread(target=ping, args=[ip])
t.start()
#!/usr/bin/env python
#coding:utf8
import
subprocess
import
threading
class
ping(object):
def__init__
(self, host):
self.host =host
def__call__
(self):
result =subprocess.call(
'ping -c2 %s &> /dev/null
' % self.host, shell=true
)if result ==0:
"%s:up
" %self.host
else
:
"%s:down
" %self.host
if__name__ == '
__main__':
ips = ('
172.40.55.%s
' % i for i in xrange(1, 255))
for ip in
ips:
t = threading.thread(target=ping(ip))
t.start()
多執行緒實現tcpserver多客戶端連線
#!/usr/bin/env python
#coding:utf8
import
threading
import
time
from socket import
socket, af_inet, sock_stream, sol_socket, so_reuseaddr
host = ''
port = 12345addr =(host, port)
defhandle_child(c_sock):
while
true:
data = c_sock.recv(1024).strip()
ifnot
data:
break
c_sock.send(
'[%s] %s\r\n
' %(time.ctime(), data))
c_sock.close()
if__name__ == '
__main__':
s =socket(af_inet, sock_stream)
s.setsockopt(sol_socket, so_reuseaddr, 1)
s.bind(addr)
s.listen(1)
while
true:
cli_sock, cli_addr =s.accept()
t = threading.thread(target=handle_child, args=(cli_sock,))
t.start()
python 多執行緒 和 多程序
單執行緒例子 usr bin python coding utf 8 name danxiancheng.py import time import threading def loop num,sec print loop s start num,time.strftime y m d h m s...
python多執行緒和多程序
pool 感謝多執行緒和多程序最大的不同在於,多程序中,同乙個變數,各自有乙份拷貝存在於每個程序中,互不影響 而多執行緒中,所有變數都由所有執行緒共享,所以,任何乙個變數都可以被任何乙個執行緒修改,因此,執行緒之間共享資料最大的危險在於多個執行緒同時改乙個變數,把內容給改亂了。python中,多執行...
多程序和多執行緒python
coding utf8 import threading import time class mop floor threading.thread def init self super mop floor,self init def run self print 我要拖地了 time.sleep ...