Allow Ray API to be used from multiple threads (#2422)

This commit is contained in:
Hao Chen
2018-07-20 15:39:01 -07:00
committed by Robert Nishihara
parent 4b6157ed09
commit 05f485e274
5 changed files with 167 additions and 93 deletions
+32
View File
@@ -4,6 +4,7 @@ import os
import re
import string
import sys
import threading
import time
import unittest
from collections import defaultdict, namedtuple, OrderedDict
@@ -1144,6 +1145,37 @@ class APITest(unittest.TestCase):
with self.assertRaises(Exception):
ray.get(3)
def testMultithreading(self):
self.init_ray(driver_mode=ray.SILENT_MODE)
@ray.remote
def f():
pass
def g(n):
for _ in range(1000 // n):
ray.get([f.remote() for _ in range(n)])
res = [ray.put(i) for i in range(1000 // n)]
ray.wait(res, len(res))
def test_multi_threading():
threads = [
threading.Thread(target=g, args=(n, ))
for n in [1, 5, 10, 100, 1000]
]
[thread.start() for thread in threads]
[thread.join() for thread in threads]
@ray.remote
def test_multi_threading_in_worker():
test_multi_threading()
# test multi-threading in the driver
test_multi_threading()
# test multi-threading in the worker
ray.get(test_multi_threading_in_worker.remote())
@unittest.skipIf(
os.environ.get('RAY_USE_NEW_GCS', False),