forked from apache/systemds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonDMLScript.java
More file actions
105 lines (89 loc) · 3.14 KB
/
PythonDMLScript.java
File metadata and controls
105 lines (89 loc) · 3.14 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sysds.api;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.sysds.api.jmlc.Connection;
import py4j.DefaultGatewayServerListener;
import py4j.GatewayServer;
import py4j.Py4JNetworkException;
public class PythonDMLScript {
private static final Log LOG = LogFactory.getLog(PythonDMLScript.class.getName());
final private Connection _connection;
public static GatewayServer GwS;
/**
* Entry point for Python API.
*
* @param args Command line arguments.
* @throws Exception Throws exceptions if there is issues in startup or while running.
*/
public static void main(String[] args) throws Exception {
final DMLOptions dmlOptions = DMLOptions.parseCLArguments(args);
DMLScript.loadConfiguration(dmlOptions.configFile);
GwS = new GatewayServer(new PythonDMLScript(), dmlOptions.pythonPort);
GwS.addListener(new DMLGateWayListener());
try {
GwS.start();
}
catch(Py4JNetworkException p4e) {
/**
* This sometimes happens when the startup is using a port already in use. In this case we handle it in python
* therefore use logging framework. and terminate program.
*/
LOG.info("failed startup", p4e);
System.exit(-1);
}
catch(Exception e) {
throw new DMLException("Failed startup and maintaining Python gateway", e);
}
}
private PythonDMLScript() {
// we enable multi-threaded I/O and operations for a single JMLC
// connection because the calling Python process is unlikely to run
// multi-threaded streams of operations on the same shared context
_connection = new Connection();
}
public static void setDMLGateWayListenerLoggerLevel(Level l){
Logger.getLogger(DMLGateWayListener.class).setLevel(l);
}
public Connection getConnection() {
return _connection;
}
protected static class DMLGateWayListener extends DefaultGatewayServerListener {
private static final Log LOG = LogFactory.getLog(DMLGateWayListener.class.getName());
@Override
public void serverPostShutdown() {
LOG.info("Shutdown done");
}
@Override
public void serverPreShutdown() {
LOG.info("Starting JVM shutdown");
}
@Override
public void serverStarted() {
LOG.info("GatewayServer started");
}
@Override
public void serverStopped() {
LOG.info("GatewayServer stopped");
}
}
}