I was trying to follow the example @ this location:
[How to use threading in Python?
I have a sample dataframe (df) like this:
segment x_coord y_coord
a 1 1
a 2 4
a 1 7
b 2 3
b 4 3
b 8 3
c 4 4
c 2 5
c 7 8
and creating kd-tree using for loop for each of segments in loop as below:
dist_name=df['segment'].unique()
for i in range(len(dist_name)):
a=df[df['segment']==dist_name[i]]
tree[i] = spatial.cKDTree(a[['x_coord','y_coord']])
How can i parallelize the tree creation using the sample sighted in link as below:
results = []
for url in urls:
result = urllib2.urlopen(url)
results.append(result)
Parallelize to >>
pool = ThreadPool(4)
results = pool.map(urllib2.urlopen, urls)
My attempt
import pandas as pd
import time
from scipy import spatial
import random
from multiprocessing.dummy import Pool as ThreadPool
dist_name=['a','b','c','d','e','f','g','h']
df=pd.DataFrame()
for i in range(len(dist_name)):
if i==0:
df['x_coord']=random.sample(range(1, 10000), 1000)
df['y_coord']=random.sample(range(1, 10000), 1000)
df['segment']=dist_name[i]
else:
tmp=pd.DataFrame()
tmp['x_coord']=random.sample(range(1, 10000), 1000)
tmp['y_coord']=random.sample(range(1, 10000), 1000)
tmp['segment']=dist_name[i]
df=df.append(tmp)
start_time = time.time()
for i in range(len(dist_name)):
a=df[df['segment']==dist_name[i]]
tree = spatial.cKDTree(a[['x_coord','y_coord']])
print("--- %s seconds ---" % (time.time() - start_time))
--- 0.0312347412109375 seconds ---
def func(name):
a = df[df['segment'] == name]
return spatial.cKDTree(a[['x_coord','y_coord']])
pool = ThreadPool(4)
start_time = time.time()
tree = pool.map(func, dist_name)
print("--- %s seconds ---" % (time.time() - start_time))
--- 0.031250953674316406 seconds ---