Python中線程和函式的區別 例項演示

2021-07-01 21:03:15 字數 3250 閱讀 8032

假設我們要檢查本地網路中哪些ip位址是可達的哪些是不可達的,我們可以用迴圈來實現,也可以用執行緒來實現。

不用執行緒的實現方式:

#!/usr/bin/python

import os, re, threading, commands, time

status = ["no respones","alive, but 50% package loss","alive","shouldn't occur"]

received_packages = re.compile(r"(\d) received")

for suffix in range(20,29):

ip = "10.170.9."+str(suffix)

ping_out = os.popen("ping -c 2 " + ip ,"r")

while true:

line = ping_out.read()

if not line:

break

n_received = re.findall(received_packages, line)

if n_received:

print "status from ", ip, " is ", status[int( n_received[0])],time.ctime(time.time())

執行結果:

status from 10.170.9.20 is alive fri apr 17 13:57:03 2015

status from 10.170.9.21 is no respones fri apr 17 13:57:06 2015

status from 10.170.9.22 is no respones fri apr 17 13:57:09 2015

status from 10.170.9.23 is no respones fri apr 17 13:57:12 2015

status from 10.170.9.24 is no respones fri apr 17 13:57:15 2015

status from 10.170.9.25 is alive fri apr 17 13:57:16 2015

status from 10.170.9.26 is alive fri apr 17 13:57:17 2015

status from 10.170.9.27 is alive fri apr 17 13:57:18 2015

status from 10.170.9.28 is no respones fri apr 17 13:57:22 2015

用執行緒的實現方式:

#!/usr/bin/python

import os, re, threading, commands, time

class ip_check(threading.thread):

def __init__(self, ip):

threading.thread.__init__(self)

self.ip = ip

self.__successful_pings = -1

def run(self):

ping_out = os.popen("ping -c 2 " + self.ip ,"r")

while true:

line = ping_out.read()

if not line:

break

n_received = re.findall(received_packages, line)

if n_received:

self.__successful_pings = int( n_received[0])

def status(self):

if self.__successful_pings == 0:

return "no respones"

elif self.__successful_pings == 1:

return "alive, but 50% package loss"

elif self.__successful_pings == 2:

return "alive"

else:

return "shouldn't occur"

received_packages = re.compile(r"(\d) received")

check_results =

for suffix in range(20,29):

ip = "10.170.9."+str(suffix)

current = ip_check(ip)

current.start()

for el in check_results:

el.join()

print "status from ", el.ip, " is ", el.status(),time.ctime(time.time())

執行結果:

status from 10.170.9.20 is alive fri apr 17 13:57:39 2015

status from 10.170.9.21 is no respones fri apr 17 13:57:41 2015

status from 10.170.9.22 is no respones fri apr 17 13:57:41 2015

status from 10.170.9.23 is no respones fri apr 17 13:57:41 2015

status from 10.170.9.24 is no respones fri apr 17 13:57:41 2015

status from 10.170.9.25 is alive fri apr 17 13:57:41 2015

status from 10.170.9.26 is alive fri apr 17 13:57:41 2015

status from 10.170.9.27 is alive fri apr 17 13:57:41 2015

status from 10.170.9.28 is no respones fri apr 17 13:57:41 2015

無線程和有執行緒的結果分析:

有執行緒的耗時是: 2秒

無線程的耗時是:19秒

執行緒的方式明顯比無線程的快將近10倍。如果在要處理的資料量更大的情況下,執行緒的處理方式的優勢更明顯。

python中線程

程序和執行緒的區別 1.程序 每個程式都會有乙個程序,負責管理程式各個功能的執行,程序只會有乙個 而且至少有乙個 相當於包工頭 2.執行緒 每個程序裡面至少有乙個執行緒,稱之為主線程,除此以外還會有其他執行緒,稱之為分執行緒 執行緒是控制任務執行的最小單位 相當於農名工 3.程序負責控制各個執行緒的...

python 中線程

import threading import time class test threading.thread 繼承threading.thread def init self super test,self init def run self 設定執行緒方法 threadname threadi...

linux中線程同步和互斥的區別

相交程序之間的關係主要有兩種,同步與互斥。所謂互斥,是指散步在不同程序之間的若干程式片斷,當某個程序執行其中乙個程式片段時,其它程序就不能執行它 們之中的任一程式片段,只能等到該程序執行完這個程式片段後才可以執行。所謂同步,是指散步在不同程序之間的若干程式片斷,它們的執行必須嚴格按照規定的某種先後次...