usr bin python coding utf-8 import time import threading import math i

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import threading
import math
import urllib
def main():
print 'File system:',
tm= T()
# создаём потоки
th_num = 30 # количество
prs = []
for i in xrange(th_num):
p = Th()
p.setName(str(i))
p.start()
# ожидаем завершения потоков
while True:
if threading.activeCount() == 1:
break
print tm.gets()
print 'Processor:',
tm= T()
# создаём потоки
th_num = 30 # количество
prs = []
for i in xrange(th_num):
p = Th1()
p.setName(str(i))
p.start()
# ожидаем завершения потоков
while True:
if threading.activeCount() == 1:
break
print tm.gets()
class Th(threading.Thread):
def run(self):
f = open('tst/%s.txt' % self.getName(), 'w')
for j in xrange(100000):
f.write(str(j))
f.close()
class Th1(threading.Thread):
def run(self):
a = []
for j in xrange(100000):
a.append(j*j*j)
class T:
round= 3 # до какого знака после запятой округлять
desc= '' # описание
def __init__(self, desc= ''):
self.desc= desc
self.start= time.time()
def get(self):
return round(time.time() - self.start, self.round)
def gets(self):
return str(self.get())
def put(self, desc=''):
if desc != '':
desc+= ': '
elif self.desc != '':
desc= self.desc + ': '
print desc + self.gets() + ' sec'
def log(self, desc=''):
if desc != '':
desc+= ': '
elif self.desc != '':
desc= self.desc + ': '
logging.info(desc + self.gets() + ' sec')
if __name__ == "__main__":
main()