Python_scripts/raster2pgsql_multithread.py
2024-05-15 17:04:52 +02:00

68 lines
2.6 KiB
Python

import logging
import threading
import time
from subprocess import run
from os import listdir,getcwd,chdir,system
def thread_func(name,table,schema):
logging.info("Thread %s: starting", name)
time.sleep(2)
if not isinstance(name,list) : name = [name]
cmd = 'export PGPASSWORD=#CEN38@venir;raster2pgsql -s 2154 -c -C -I -M -t 5x5 -N 0 {file} {sch}.{tab} | psql -h 91.134.194.221 -U cen_admin -d azalee'.format(file=" ".join(name),sch=schema,tab=table)
system(cmd)
logging.info("Thread %s: finishing", name)
def thread_function(name,table,schema):
logging.info("Thread %s: starting", name)
time.sleep(2)
if not isinstance(name,list) : name = [name]
cmd = 'export PGPASSWORD=#CEN38@venir;raster2pgsql -s 2154 -a -t 5x5 {file} {sch}.{tab} | psql -h 91.134.194.221 -U cen_admin -d azalee'.format(file=" ".join(name),sch=schema,tab=table)
system(cmd)
logging.info("Thread %s: finishing", name)
def last_thread_function(name,table,schema):
logging.info("Thread %s: starting", name)
time.sleep(2)
if not isinstance(name,list) : name = [name]
cmd = 'export PGPASSWORD=#CEN38@venir;raster2pgsql -s 2154 -a -C -I -M -t 5x5 {file} {sch}.{tab} | psql -h 91.134.194.221 -U cen_admin -d azalee'.format(file=" ".join(name),sch=schema,tab=table)
system(cmd)
logging.info("Thread %s: finishing", name)
if __name__ == "__main__":
format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO,
datefmt="%H:%M:%S")
# reste 38
workspace = r'/home/colas/Documents/8_SIG/MNT/IGN - RGE Alti 5M/38'
chdir(workspace)
list_f = listdir()
list_asc = ["/vsizip/"+f+"/"+f.split('.')[0]+"_MNT_LAMB93_IGN69.asc" for f in list_f]
# Création d'une séquence au pas de 70
seq = list(range(0,len(list_asc),70))
end_seq = len(list_asc)
for i,j in enumerate(seq):
k = end_seq if i == len(seq)-1 else seq[i+1]
print(len(list_asc[j:k]))
threads = list()
for file in list_asc[j:k]:
logging.info("Main : create and start thread %s.", file)
if file == list_asc[-1]:
x = threading.Thread(target=last_thread_function, args=(file,'mnt_5m','ref_territoire'))
else:
x = threading.Thread(target=thread_function, args=(file,'mnt_5m','ref_territoire'))
threads.append(x)
x.start()
# [t.start() for t in threads]
[t.join() for t in threads]
# for file, thread in enumerate(threads):
# logging.info("Main : before joining thread %s.", file)
# thread.join()
# logging.info("Main : thread %s done", file)