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

  • SEARCH
  • Home
  • 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 8670947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:49:38+00:00 2026-06-12T18:49:38+00:00

I’m fairly new to python and am making a script that allows one to

  • 0

I’m fairly new to python and am making a script that allows one to bring point cloud data from other programs into Autodesk Maya. I have my script functioning fine but what i’m trying to do is make it faster. I have a for loop that iterates through a list of numbered files. I.e. datafile001.txt, datafile002.txt and so on. Is what i’m wondering is if there is a way to have it to do more then one at a time, possibly using threads or a queue? Below I have the code i have been working on:

     def threadedFuntion(args):
         if len(sourceFiles) > 3:
             for count, item in enumerate(sourceFiles):
                     t1=Thread(target=convertPcToPdc,args=(sourceFiles[filenumber1], particlesName, startframe, endframe, pdcIncrements, outputDirectory, variableFolder, acceptableArrayforms, dataType))
                     t1.start()
                     t2=Thread(target=convertPcToPdc,args=(sourceFiles[filenumber2], particlesName, startframe, endframe, pdcIncrements, outputDirectory, variableFolder, acceptableArrayforms, dataType))
                     t2.start()
                     t3=Thread(target=convertPcToPdc,args=(sourceFiles[filenumber3], particlesName, startframe, endframe, pdcIncrements, outputDirectory, variableFolder, acceptableArrayforms, dataType))
                     t3.start()
                     t4=Thread(target=convertPcToPdc,args=(sourceFiles[filenumber4], particlesName, startframe, endframe, pdcIncrements, outputDirectory, variableFolder, acceptableArrayforms, dataType))
                     t4.start()

This obviously doesn’t work for a number of reasons, first it only will create 4 threads, I would like to be able to give an option for more or less. Second it errors because it’s trying to reuse a thread? Like I said i’m quite new to python and am a little over my head, I’ve been reading several posts on here but can’t get one to work quite right. I think a queue might be something I need but couldn’t quite figure it out, I experimented with the condition statement and with the join statement, but once again couldn’t get what I want.

I guess to be more specific what I want to achieve is that the function is reading through a text file, retrieving coords and then exporting them as a binary file for maya to read. It’s common for one of these text files to have 5-10 million x,y,z coords which takes quite some time. It takes around 30mins-1hour to do 1 file on a pretty beastly computer, task manager says python is only using 12% processor and around 1% ram, so if I could do multiple of these at once, it would make doing those 100 or more files go by a lot faster. I wouldn’t think it would be to hard to multithread/queue up a for loop, but I’ve been lost and trying failing solutions for about a week.

Thank you all for any help, I really appreciate it and think this website is amazing. This is my first post, but I feel like I have completely learned python just from reading on here.

  • 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-12T18:49:39+00:00Added an answer on June 12, 2026 at 6:49 pm

    Subclass threading.Thread and put your work function in that class as part of run().

    import threading
    import time
    import random
    
    class Worker(threading.Thread):
        def __init__(self, srcfile, printlock,**kwargs):
            super(Worker,self).__init__(**kwargs)
            self.srcfile = srcfile
            self.lock = printlock # so threads don't step on each other's prints
    
        def run(self):
            with self.lock:
                print("starting %s on %s" % (self.ident,self.srcfile))
            # do whatever you need to, return when done
            # example, sleep for a random interval up to 10 seconds
            time.sleep(random.random()*10)
            with self.lock:
                print("%s done" % self.ident)
    
    
    def threadme(srcfiles):
        printlock = threading.Lock()
        threadpool = []
        for file in srcfiles:
            threadpool.append(Worker(file,printlock))
    
        for thr in threadpool:
            thr.start()
    
        # this loop will block until all threads are done
        # (however it won't necessarily first join those that are done first)
        for thr in threadpool:
            thr.join()
    
        print("all threads are done")
    
    if __name__ == "__main__":
        threadme(["abc","def","ghi"])
    

    As requested, to limit the number of threads, use the following:

    def threadme(infiles,threadlimit=None,timeout=0.01):
        assert threadlimit is None or threadlimit > 0, \
               "need at least one thread";
        printlock = threading.Lock()
        srcfiles = list(infiles)
        threadpool = []
    
        # keep going while work to do or being done
        while srcfiles or threadpool:
    
            # while there's room, remove source files
            # and add to the pool
            while srcfiles and \
               (threadlimit is None \
                or len(threadpool) < threadlimit):
                file = srcfiles.pop()
                wrkr = Worker(file,printlock)
                wrkr.start()
                threadpool.append(wrkr)
    
            # remove completed threads from the pool
            for thr in threadpool:
                thr.join(timeout=timeout)
                if not thr.is_alive():
                    threadpool.remove(thr)
    
        print("all threads are done")
    
    if __name__ == "__main__":
        for lim in (1,2,3,4):
            print("--- Running with thread limit %i ---" % lim)
            threadme(("abc","def","ghi"),threadlimit=lim)
    

    Note that this will actually process the sources in reverse (due to the list pop()). If you require them to be done in order, reverse the list somewhere, or use a deque and popleft().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into

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.