I'm trying to update a piece of code, and the initializer value/function used for multiprocessing.Pool() is causing an AttributeError when I run it. I've had a look through the documentation and a stack of relevant(ish) Google results, but I can't find anything explaining what the initializer can and can't be or any significant detail on how to use it. How do I find more information on this?
EDIT: The code up trying to update is this snippet from Real Python's concurrency article.
import requests
import multiprocessing
import time
session = None
def set_global_session():
global session
if not session:
session = requests.Session()
def download_site(url):
with session.get(url) as response:
name = multiprocessing.current_process().name
print(f"{name}:Read {len(response.content)} from {url}")
def download_all_sites(sites):
with multiprocessing.Pool(initializer=set_global_session) as pool:
pool.map(download_site, sites)
if __name__ == "__main__":
sites = [
"https://www.jython.org",
"http://olympus.realpython.org/dice",
] * 80
start_time = time.time()
download_all_sites(sites)
duration = time.time() - start_time
print(f"Downloaded {len(sites)} in {duration} seconds")
When I run that it's just repeatedly outputting "AttributeError: Can't get attribute 'set_global_session' on ".