Project

General

Profile

Bug #10009 ยป test.py

ariveira (Alexandre Riveira), 10/29/2014 01:23 PM

 
1
import thread
2
import time
3
import sys
4
import psycopg2
5

    
6
postgres = False
7
if len(sys.argv) > 1 and sys.argv[1] == '--postgres':
8
  postgres = True
9

    
10

    
11
inicio_geral = time.time()
12

    
13
result = {"1": 0, "2": 0, "3": 0}
14

    
15
def looping(key):
16
  while True:
17
    result[key] += 1
18

    
19
def database(key):
20
  conn = psycopg2.connect("\
21
    dbname='postgres'\
22
    user='postgres'\
23
    host='localhost'\
24
    password='abc123'\
25
  ");
26
  while True:
27
    c = conn.cursor()
28
    c.execute("SELECT * FROM pg_stat_activity")
29
    records = c.fetchall()
30
    result[key] += 1
31

    
32
thread.start_new_thread( looping, ("1", ) )
33

    
34
count = 0
35
while count < 10:
36
  inicio = time.time()  
37
  count += 1
38
  time.sleep(1)
39
  print('faltando ' + str(count) + ' segundos (' + str(time.time() - inicio) + ')')
40

    
41

    
42
print("zerando thread com contagem em " + str(result['1']))
43
result['1'] = 0
44

    
45
if postgres:
46
  label = "postgres "
47
  thread.start_new_thread( database, ("2", ) )
48
else:
49
  label = "second "
50
  thread.start_new_thread( looping, ("2", ) )
51

    
52
thread.start_new_thread( looping, ("3", ) )
53

    
54
totalizador = 0
55
while totalizador < 10:
56
  print("first " + str(result['1']))
57
  print(label + str(result['2']))
58
  print("third " + str(result['3']))
59
  print("\n\n")
60
  totalizador += 1
61
  time.sleep(1)
62

    
63
total = result['1'] + result['2'] + result['3']
64
print("total " + str(total))
65
print("media " + str(total/(time.time() - inicio_geral)))
66
say = 'Python Elapsed %.02f' % (time.time() - inicio_geral)
67
print(say)
68
sys.exit()