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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:41:57+00:00 2026-05-26T19:41:57+00:00

import lxml.html import mechanize, cookielib import multiprocessing browser = None def download(i): link =

  • 0
import lxml.html
import mechanize, cookielib
import multiprocessing

browser = None

def download(i):
    link = 'www.google.com'
    response = browser.open(link)
    tree = lxml.html.parse(response)
    print tree
    return 0

if __name__ == '__main__':    
    browser = mechanize.Browser()
    cookie_jar = cookielib.LWPCookieJar()
    browser.set_cookiejar(cookie_jar)
    browser.set_handle_equiv(True)
    browser.set_handle_gzip(True)
    browser.set_handle_redirect(True)
    browser.set_handle_referer(False) #inicialmente estava on mas deve ser melhor off
    browser.set_handle_robots(False)
    browser.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
    browser.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:2.0.1) Gecko/20100101 Ubuntu/11.04 maverick Firefox/4.0.1')]

    pool = multiprocessing.Pool(None)
    tasks = range(8)
    r = pool.map_async(download, tasks)
    r.wait() # Wait on the results

If I remove the multiprocessing part, it works. If I don’t call the browser inside the download function, it also works. However, it seems as if multiprocessing + mechanize is simply not working.

How can I fix this? It doesn’t happen under linux.

  • 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-26T19:41:57+00:00Added an answer on May 26, 2026 at 7:41 pm

    Only the main process executes the gated if __name__ == '__main__' block. Since Windows lacks a fork system call, each process created in the pool needs its own browser. You can do this with an initializer function. For reference, see the initializer and initargs options of multiprocessing.Pool.

    import lxml.html
    import mechanize, cookielib
    import multiprocessing as mp
    
    def download(i):
        link = 'http://www.google.com'
        response = browser.open(link)
        tree = lxml.html.parse(response)
        print tree
        return 0
    
    def init(count):
        global browser
        browser = mechanize.Browser()
        cookie_jar = cookielib.LWPCookieJar()
        browser.set_cookiejar(cookie_jar)
        browser.set_handle_equiv(True)
        browser.set_handle_gzip(True)  #warning
        browser.set_handle_redirect(True)
        browser.set_handle_referer(False)
        browser.set_handle_robots(False)
        browser.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), 
                                   max_time=1)
        browser.addheaders = [('User-agent', 
            'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:2.0.1) '
            'Gecko/20100101 Ubuntu/11.04 maverick Firefox/4.0.1')]
    
        count.value -= 1
    
    if __name__ == '__main__':
        import time
        count = mp.Value('I', mp.cpu_count())
        pool = mp.Pool(count.value, initializer=init, initargs=(count,))
        #wait until all processes are initialized
        while count.value > 0:
            time.sleep(0.1)
    
        tasks = range(8)
        r = pool.map_async(download, tasks)
        r.wait()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This small program: from lxml.html import tostring, fromstring e = fromstring(''' <html><head> <link href="/comments.css"
I am using python lxml library to parse html pages: import lxml.html # this
import urllib2 import urllib from lxml.html import fromstring from lxml.html.clean import Cleaner from formatter
Consider the following snippet: import lxml.html html = '<div><br />Hello text</div>' doc = lxml.html.fromstring(html)
In [1]: from lxml import etree I've got an HTML document: In [2]: root
import lxml.html, sys for arg in sys.argv: url2 = arg t = lxml.html.parse(url2) if
If I do this: import lxml in python, lxml.html is not imported. For instance,
Here's the code I have: from cStringIO import StringIO from lxml import etree xml
import threading event = threading.Event() event.set() print event.wait(1) None event.clear() print event.wait(1) None So
import wx class MainFrame(wx.Frame): def __init__(self,parent,title): wx.Frame.__init__(self, parent, title=title, size=(640,480)) self.mainPanel=DoubleBufferTest(self,-1) self.Show(True) class DoubleBufferTest(wx.Panel):

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.