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 6380683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:17:58+00:00 2026-05-25T02:17:58+00:00

import os import urllib import workerpool from datetime import datetime class DownloadJob(workerpool.Job): def __init__(self,

  • 0
import os
import urllib
import workerpool

from datetime import datetime

class DownloadJob(workerpool.Job):    

    def __init__(self, fa):
       self.fa = fa 

    def run(self):        
       f = open(self.fa + '.txt','w')
       f.write('Example Note.......')
       f.close()  


pool = workerpool.WorkerPool(size=5)

def workfile():        
    range1 = 51
    range2 = 102
    fam1 = 555
    fam2 = 833

    ranges = range2 -range1
    fams = fam2 -fam1
    workname = "Python"

    path = os.getcwd()

    os.system('mkdir ' + str(workname))
    sTime = datetime.now()

    for a in range(ranges + 1):
        os.chdir(path + '\\' + str(workname))
        os.system('mkdir ' + str(range1 + a))
        os.chdir(path + '\\' + str(workname) + '\\' + str(range1 + a))

        for b in range(fams + 1):
            fa = str(fam1 + b)
            job = DownloadJob(fa)
            pool.put(job)
            pool.shutdown()
            pool.wait()                                              

        print 'Elapsed Time: %s' % (datetime.now() - sTime)

        z = open('info.txt','w')
        z.write('Elapsed Time: %s' % (datetime.now() - sTime))
        z.close()

    os.chdir(path + '\\' + str(workname))

    tumSure = open('info.txt','w')
    tumSure.write('Elapsed All Time: %s' % (datetime.now() - sTime))
    tumSure.close()

    print 'All Time: %s' % (datetime.now() - sTime)        
    print 'Workname : %s downloaded.' % (workname)

    quit()

workfile()

Hi all,

I have a code as above and I want to use thread logic for creating file. Folder numbers start wird range1, i.e. 51. Text files are created in this directory with the names 555.txt to 833.txt. But after creating the folder with name 52 it stops, failing to create 555.txt to 833.txt.

it think it stops because

pool.shutdown()
pool.wait()

How can I make the loop continue with no stop?

  • 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-05-25T02:17:59+00:00Added an answer on May 25, 2026 at 2:17 am

    I think you should only shutdown the pool if you are finished with it, i.e. after the for a loop, maybe even in a try...finally clause.

    It would look that way:

    try:
        for a in range(ranges + 1):
            os.chdir(path + '\\' + str(workname))
            os.system('mkdir ' + str(range1 + a))
            os.chdir(path + '\\' + str(workname) + '\\' + str(range1 + a))
    
            for b in range(fams + 1):
                fa = str(fam1 + b)
                job = DownloadJob(fa)
                pool.put(job)
    finally:
        pool.shutdown()
        pool.wait()
    

    In this way the pool shutdown

    a) happens only if all pool putting is done and
    b) happens even if there is an exception in order to cleanly shutdown.

    If the pool had a context manager, it would be even simpler. But AFAICS, it hasn’t.
    Otherwise, you could do

    with pool:
        for a in range(ranges + 1):
            os.chdir(path + '\\' + str(workname))
            os.system('mkdir ' + str(range1 + a))
            os.chdir(path + '\\' + str(workname) + '\\' + str(range1 + a))
    
            for b in range(fams + 1):
                fa = str(fam1 + b)
                job = DownloadJob(fa)
                pool.put(job)
    

    But if you want, you can do

    from contextlib import contextmanager
    
    @contextmanager
    def shutdown_wait(pool):
        try:
            yield pool
        finally:
            pool.shutdown()
            pool.wait()
    
    ...
    with shutdown_wait(pool):
        for a ... [as above]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import urllib.parse import urllib.request import time def __init__(self, parent= None): QtGui.QWidget.__init__(self,parent) self.ui = Ui_MainWindow()
I have a Python file with as content: import re import urllib class A(object):
import urllib import xml.etree.ElementTree as ET def getWeather(city): #create google weather api url url
import urllib2 import urllib from BeautifulSoup import BeautifulSoup # html from BeautifulSoup import BeautifulStoneSoup
This is my code: from xgoogle.search import GoogleSearch, SearchError import urllib, urllib2, sys, argparse
import urllib2 import urllib from lxml.html import fromstring from lxml.html.clean import Cleaner from formatter
import json import urllib import re import binascii def asciirepl(match): s = match.group() return
import urllib, urllib2 def URLRequest(url, params, method=GET): if method == POST: return urllib2.Request(url, data=urllib.encode(params))
How to get cyrillic string from document? I have fallowing code: import urllib from
If I use Urllib2 to open a url using this: import urllib import urllib2

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.