Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:16:51+00:00 2026-06-14T18:16:51+00:00

Hopefully someone can help, I have a challenging situation that I cannot not seem

  • 0

Hopefully someone can help, I have a challenging situation that I cannot not seem to script for. My aim is to automate loading SQL files into PostgreSQL.

I wont know how many folders of SQL files I have so intially I check a folder exists and then loop through each file and load it into PostgreSQL using psql.exe

My current code looks like this

if os.path.exists("sql1"):
    for files in os.listdir("sql1"):    
    load1 = subprocess.Popen("psql -d data -U postgres -f sql1\%s" %files)


if os.path.exists("sql2"):
    for files in os.listdir("sql2"):    
    load2 = subprocess.Popen("psql -d data -U postgres -f sql2\%s" %files)

However this spools so many subprocesses as it creates a subprocess for each SQL file in the folder as well as more subprocesses for each folder.

If I change it to a subprocess.call it will of course seriliase the loading and block loading the files from the next folder, rather than running a single process for each folder.

Does anyone know how I could create a single process for each folder that exists?

In addition to this I will then run the indexes but only once all processes have finished.

I could use load.wait() but that would only work for one process.

thanks for advice and help in advance

EDIT ADDED:

Taking Steve’s advice I introduced some threads but it still causes the indexing to start before the subprocesses have finished

def threads(self):
  processors = multiprocessing.cpu_count()
  n = 1
  name = "sql%i" %n
  for i in range(processors):
      if os.path.exists(name):
     thread = Thread(target=self.loadData, args=(name,))
     thread.start()             
     n += 1
     name = "sql%i" %n

 def loadData(self, name):
    for files in os.listdir(name):
    load = subprocess.Popen("psql -d osdata -U postgres -f %s\%s" %(name, files))
    load.wait()                                                                                                                                                                                 

But the indexing starts before the processes have finished.

Any ideas how to prevent that

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:16:53+00:00Added an answer on June 14, 2026 at 6:16 pm

    I would suggest creating a thread for each folder. Then use subprocess.call to serialise calls within each thread.

    If you want to throttle the number of threads executing concurrently, you should look at Python’s futures module.

    http://docs.python.org/dev/library/concurrent.futures.html

    from concurrent.futures import ThreadPoolExecutor
    
    with ThreadPoolExecutor(max_workers=2) as executor:
        for n in range(processors):
            name = "sql%i" % (n + 1)
            if os.path.exists(name):
                future = executor.submit(loadData, name)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have another beginner's question that hopefully someone can help with. I'm trying to
Hopefully someone can help here. I have a ListView that is populated by a
Not too familiar with GSA here so hopefully someone can help me. Have a
Hopefully someone can help here, it's incredibly frustrating! I have a couple of iOS
everyone! i have a question about sessions hopefully someone can help me with. I
Quite a simple one I think, but hopefully someone can help out. I have
hopefully someone can help me with this headache I have. I'm currently running Drupal
hopefully someone can help me with this odd problem. I have a database which
Hopefully someone can help me with a slight problem/confusion I have with Viewpagers and
Hopefully someone can help me with my problem. Background I have create an InfoPath

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.