Skip to content

Commit 1dc8c87

Browse files
committed
Added pool to Thread executor
1 parent c3b08e1 commit 1dc8c87

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

graphql/execution/executors/thread.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from multiprocessing.pool import ThreadPool
12
from threading import Thread
23

34
from ...pyutils.aplus import Promise
@@ -6,16 +7,28 @@
67

78
class ThreadExecutor(object):
89

9-
def __init__(self):
10+
pool = None
11+
12+
def __init__(self, pool=False):
1013
self.threads = []
14+
if pool:
15+
self.execute = self.execute_in_pool
16+
self.pool = ThreadPool(processes=pool)
17+
else:
18+
self.execute = self.execute_in_thread
1119

1220
def wait_until_finished(self):
1321
for thread in self.threads:
1422
thread.join()
1523

16-
def execute(self, fn, *args, **kwargs):
24+
def execute_in_thread(self, fn, *args, **kwargs):
1725
promise = Promise()
1826
thread = Thread(target=process, args=(promise, fn, args, kwargs))
1927
thread.start()
2028
self.threads.append(thread)
2129
return promise
30+
31+
def execute_in_pool(self, fn, *args, **kwargs):
32+
promise = Promise()
33+
self.pool.map(lambda input: process(*input), [(promise, fn, args, kwargs)])
34+
return promise

0 commit comments

Comments
 (0)