I am trying to use a global list which can be appended when a thread/process finishes a task. My main thread can read from this but by function can not append it. Basically im making requests to get working proxies and then trying to save them to the list and then print the list out at the end. I have cut out as much as possible.
goodProxyList = ["test"]
def testProxy(x):
global goodProxyList
try:
test = requests.get('http://someurl.com/', proxies=proxies, timeout=10)
if test.status_code == 200:
goodProxyList.append(x)
else:
print("Something went wrong! :/" + " From PID: " + str(pid))
except:
print("SOMETHING WENT VERY WRONG" + " From PID: " + str(pid))
if __name__ == '__main__':
##Setup Stuff happens
p=Pool(2)
p.map(testProxy, proxyList)
for i in goodProxyList:
print(i)
Even if I change goodProxyList.append(x) to goodProxyList.append("Anything"), the last 2 lines still onlt output "test". What am I doing wrong?
EDIT:
I have found the answer through help from brianpck. As he says, it seems processes work differently from threads. My changing to a pool thread it now works perfectly.
#p=Pool(2)
#p.map(testProxy, proxyList)
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.map(testProxy, proxyList)