Creating 50K text files in python

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Mar 18 09:17:57 EDT 2009


En Wed, 18 Mar 2009 11:40:26 -0200, venutaurus539 at gmail.com  
<venutaurus539 at gmail.com> escribió:
> On Mar 18, 6:16 pm, "venutaurus... at gmail.com"
> <venutaurus... at gmail.com> wrote:

>>           I've an application where I need to create 50K files spread
>> uniformly across 50 folders in python. The content can be the name of
>> file itself repeated 10 times.I wrote a code using normal for loops
>> but it is taking hours together for that. Can some one please share
>> the code for it using Multithreading. As am new to Python, I know
>> little about threading concepts.
>>
> I managed to get this code but the problem is main thread exits before
> the completetion of child threads:
>
> class MyThread ( threading.Thread ):
>
>    def run ( self ):
>
>       global theVar
>       print 'This is thread ' + str ( theVar ) + ' speaking.'
>       print 'Hello and good bye.'
>       theVar = theVar + 1
>       time.sleep (4)
>       print 'This is thread ' + str ( theVar ) + ' speaking. again'
>
> for x in xrange ( 20 ):
>    MyThread().start()

You have to .join() each thread. Replace the above two lines with:

threads = [MyThread() for x in range(20)]
for thread in threads:
   thread.start()
for thread in threads:
   thread.join()

But this is unrelated to your main problem. You don't have to use threads  
for this.

-- 
Gabriel Genellina




More information about the Python-list mailing list