forked from dask/distributed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_preload.py
68 lines (51 loc) · 1.63 KB
/
test_preload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import shutil
import sys
import tempfile
from distributed import Client
from distributed.utils_test import cluster
from distributed.utils_test import loop # noqa F401
PRELOAD_TEXT = """
_worker_info = {}
def dask_setup(worker):
_worker_info['address'] = worker.address
def get_worker_address():
return _worker_info['address']
"""
def test_worker_preload_file(loop):
def check_worker():
import worker_info
return worker_info.get_worker_address()
tmpdir = tempfile.mkdtemp()
try:
path = os.path.join(tmpdir, "worker_info.py")
with open(path, "w") as f:
f.write(PRELOAD_TEXT)
with cluster(worker_kwargs={"preload": [path]}) as (s, workers), Client(
s["address"], loop=loop
) as c:
assert c.run(check_worker) == {
worker["address"]: worker["address"] for worker in workers
}
finally:
shutil.rmtree(tmpdir)
def test_worker_preload_module(loop):
def check_worker():
import worker_info
return worker_info.get_worker_address()
tmpdir = tempfile.mkdtemp()
sys.path.insert(0, tmpdir)
try:
path = os.path.join(tmpdir, "worker_info.py")
with open(path, "w") as f:
f.write(PRELOAD_TEXT)
with cluster(worker_kwargs={"preload": ["worker_info"]}) as (
s,
workers,
), Client(s["address"], loop=loop) as c:
assert c.run(check_worker) == {
worker["address"]: worker["address"] for worker in workers
}
finally:
sys.path.remove(tmpdir)
shutil.rmtree(tmpdir)