I an trying to run this Python code on several threads of my processor, but I can't find how to allocate multiple threads. I am using python 2.7 in Jupyter (formerly IPython).
The initial code is below (all this part works perfectly). It is a web parser which takes x
i.e., a url among my_list i.e., a list of url and then write a CSV (where out_string is a line).
Code without MultiThreading
my_list = ['http://stackoverflow.com/', 'http://google.com']
def main():
with open('Extract.csv'), 'w') as out_file:
count_loop = 0
for x in my_list:
#================ Get title ==================#
out_string = ""
campaign = parseCampaign(x)
out_string += ';' + str(campaign.getTitle())
#================ Get Profile ==================#
if campaign.getTitle() != 'NA':
creator = parseCreator(campaign.getCreatorUrl())
out_string += ';' + str(creator.getCreatorProfileLinkUrl())
else:
pass
#================ Write ==================#
out_string += '\n'
out_file.write(out_string)
count_loop +=1
print '---- %s on %s ------- ' %(count_loop, len(my_list))
Code with MultiThreading but not working
from threading import Thread
my_list = ['http://stackoverflow.com/', 'http://google.com']
def main(x):
with open('Extract.csv'), 'w') as out_file:
count_loop = 0
for x in my_list:
#================ Get title ==================#
out_string = ""
campaign = parseCampaign(x)
out_string += ';' + str(campaign.getTitle())
#================ Get Profile ==================#
if campaign.getTitle() != 'NA':
creator = parseCreator(campaign.getCreatorUrl())
out_string += ';' + str(creator.getCreatorProfileLinkUrl())
else:
pass
#================ Write ==================#
out_string += '\n'
out_file.write(out_string)
count_loop +=1
print '---- %s on %s ------- ' %(count_loop, len(my_list))
for x in my_list:
t = Thread(target=main, args=(x,))
t.start()
t2 = Thread(target=main, args=(x,))
t2.start()
I cannot find a good way to implement more than one thread to run this piece of code, and I am a bit confused because the documentation is not very easy to understand. With one core, this code takes 2 hours long, multi-threading will save me lot of time!