Advertisement
Guest User

AHHHHH

a guest
Feb 5th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | None | 0 0
  1. #!/usr/local/bin/python
  2.  
  3. import os
  4. import psycopg2
  5. import sys
  6. import time
  7. import datetime
  8.  
  9. ts = time.time()
  10. amount = 250
  11. process = 25
  12.  
  13.  
  14. def child(from_id, to_id):
  15.     print ("\nChild start", os.getpid())
  16.     print datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
  17.     try:
  18.         con = psycopg2.connect(host='ohm.f4.htw-berlin.de', port='5432', database='ohdm', user='mholst', password='master85')
  19.         cur = con.cursor()
  20.         cur.execute("SELECT lg.id, lg.multilinestring_geom, gd.valid_since, gd.valid_since, gt.key, gt.value FROM ohdm.multilinestring_geom lg JOIN ohdm.geographic_geom_dates gd ON gd.id_multilinestring_geom = lg.id JOIN ohdm.geographic_objects gobjects ON gobjects.id = gd.id_geographic_object_source JOIN ohdm.geographic_tag gt ON gt.id_geographic_objects = gobjects.id WHERE lg.id >= %s AND lg.id <= %s",(from_id, to_id))
  21.        
  22.         result = cur.fetchall()
  23.         for r in result:
  24.             #cur.execute("INSERT INTO ohdm.rendering_line_mholst (multilinestring_geom, valid_since, valid_until) VALUES (r[0], r[1], r[2])")
  25.             #print("INSERT INTO ohdm.rendering_line_mholst (multilinestring_geom, valid_since, valid_until) VALUES (" + r[0] + ", " + r[1] + ", " + r[2] +") ")
  26.             print(r[0])
  27.  
  28.         con.commit()
  29.     except psycopg2.DatabaseError, e:
  30.    
  31.         if con:
  32.             con.rollback()
  33.    
  34.         print 'Error %s' % e    
  35.         sys.exit(1)
  36.    
  37.     finally:
  38.    
  39.         if con:
  40.             con.close()
  41.             os._exit(0)  
  42.  
  43. def parent():
  44.    from_id = 1
  45.    to_id = 250
  46.    i = 0
  47.    for i in range(0,process):
  48.       pid = os.fork()
  49.       if pid == 0:
  50.          child(from_id, to_id)
  51.       else:
  52.          pids = (os.getpid(), pid)
  53.          print("parent: %d, child: %d\n" % pids)
  54.       from_id = from_id + 10
  55.       to_id = to_id + 10
  56.       if to_id >= amount:
  57.          to_id = amount
  58.       i=i+1
  59.  
  60. print "lineImporter is starting now!"
  61.  
  62. parent()
  63.  
  64. print ("the procedure is finally over!")
  65. print datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement