Python之多进程和多线程

目标:

  1.os.fork简单示例

  2.使用os.fork多进程测试IP是否在线

  3.使用os.fork多进程解决tcpserver多客户端连接问题

  4.多线程测试IP地址是否在线

1.os.fork简单示例

代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os

pid = os.fork()
print pid
print '--------------------'

if pid:
    print pid,'from parent'
else:
    print pid,'from child'
*提示:子进程执行从os.fork()开始
 
2.使用os.fork多进程测试IP是否在线
代码如下:
#!/usr/bin/env python
#coding:utf8

import os
import subprocess
import sys

ips = ('192.168.80.%s' % i for i in xrange(1, 255)) #生成器表达式

for ip in ips:
    pid = os.fork()
    if not pid:
        return_val = subprocess.call('ping -c1 %s &> /dev/null' %ip, shell=True)
        if return_val == 0:
            print "%s:up" % ip
        else:
            print "%s:down" % ip
        sys.exit(0)

 

3.使用os.fork多进程解决tcpserver多客户端连接问题
代码如下:
 
#!/usr/bin/env python 
#coding:utf8

import os
import time
from socket import socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR
import sys

host = ''
port = 12345
addr = (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()
    if pid:
        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()
            if not data:
                break
            cli_sock.send('[%s] %s\r\n' %(time.ctime(), data))
        cli_sock.close()
        sys.exit(0)
s.close()
 
4.多线程测试IP地址是否在线
代码如下:
 
#!/usr/bin/env python
#coding:utf8

import subprocess
import threading


def ping(host):
    result = subprocess.call(
        'ping -c2 %s &> /dev/null' % host, shell=True
    )

    if result == 0:
        print "%s:up" % host
    else:
        print "%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:
            print "%s:up" % self.host
        else:
            print "%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 = 12345
addr = (host, port)

def handle_child(c_sock):
    while True:
        data = c_sock.recv(1024).strip()
        if not 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()
        

 

posted @ 2017-01-17 19:01  xkops  阅读(337)  评论(0编辑  收藏  举报