LoginSignup
8
4

More than 5 years have passed since last update.

VirtualBoxのLinuxにGraalVMを入れてpythonのスクリプトを動かしてみた。

Last updated at Posted at 2018-04-23

GraalVM、かなりアツいOSSですね。各所で話題になってます。

オラクル、JavaやJavaScript、Ruby、Pythonなど多言語対応を単一ランタイムで実現する「GraalVM」をオープンソースで公開。Twitterが本番環境で採用
http://www.publickey1.jp/blog/18/javajavascriptrubypythongraalvmtwitter.html

導入も簡単だったので置いときますやね彡(゚)(゚)

GraalVMのダウンロード&インストール

下記のリンク(Getting start) ⇒ Downloads ⇒ Community Edition (CE) で
「graalvm-ce-1.0.0-rc1-linux-amd64.tar.gz」をダウンロード

GraalVM Getting start
https://www.graalvm.org/docs/getting-started/

Community Edition (CE)
https://www.graalvm.org/downloads/

VirtualBoxのゲストOS(Linux、本記事ではOEL6を使用)の適当なディレクトリに展開する。
今回は「/home/oracle/work」配下に展開。超簡単やね。彡(゚)(゚)

tar xvzf graalvm-ce-1.0.0-rc1-linux-amd64.tar.gz

Graal Pythonコンポーネントのインストール

GraalVMのbinディレクトリにPATHを通す。オプションでJAVA_HOMEもGraalVMの展開ディレクトリにセット。

export PATH=/home/oracle/work/graalvm-1.0.0-rc1/bin:$PATH
export JAVA_HOME=/home/oracle/work/graalvm-1.0.0-rc1

マニュアルの通りに guコマンド(Graal updater)でpython componentをインストール

gu -c install org.graalvm.python

Downloading: Component catalog
Processing component archive: Component org.graalvm.python
Downloading: Component org.graalvm.python
Installing new component: Graal.python (org.graalvm.python, version 1.0.0-rc1)

Graal Pythonの実行

下記マニュアルを参考にしつつ、まずはコマンドラインから。

GraalVM - Python 3
https://www.graalvm.org/docs/reference-manual/languages/python/#python-3

graalpythonコマンドでコマンドラインを起動。
VMを起動しているせいなのか、print文の初めの1回は遅い彡(゚)(゚)

graalpython

Please note: This Python implementation is in the very early stages, and can run little more than basic benchmarks at this point.
>>> print ("1+2=", 1 + 2)
1+2= 3
>>> print ("1+2=", 1 + 2)
1+2= 3

次に下記のpythonスクリプトを動かしてみます。

cat py3test.py

# -*- coding: utf-8 -*-

print("Hello World!")

for i in range(0,6):
    print(i,"Hello World!")

for i in range(0,6):
    if i%2==0:
        print(i," is even number.")
    else:
        print(i," is odd number.")

スクリプトの実行コマンドは下記、こいつ……動くぞ!彡(゚)(゚)

graalpython --jvm --polyglot py3test.py

Please note: This Python implementation is in the very early stages, and can run little more than basic benchmarks at this point.
Hello World!
0 Hello World!
1 Hello World!
2 Hello World!
3 Hello World!
4 Hello World!
5 Hello World!
0  is even number.
1  is odd number.
2  is even number.
3  is odd number.
4  is even number.
5  is odd number.

うーん、こいつは凄い。他の言語も動くようだし、polyglotって機能で
言語が混ぜこぜでも同時に動くらしい。試してみないと。。。彡(゚)(゚)

JavaモジュールのNative Image化(※4/25追記)

python実行とは直接関係ないのですが、GraalVMにはNative Image化という
高速化という機能が有り、試してみたのでメモ。

まずワイ環境ではnative-imageコマンドの実行に、
gccとzlib-develのインストールが必要でした彡(゚)(゚)

yum install gcc
yum install zlib-devel

以下のjavaをコンパイル&Native Image化彡(゚)(゚)

cat HelloGraalJava.java

import org.graalvm.polyglot.*;

class HelloGraalJava {
    public static void main(String[] args) {
      System.out.println("Hello, Graal Java!");
    }
}

javac HelloGraalJava.java

native-image HelloGraalJava

Build on Server(pid: 3847, port: 26682)
   classlist:     909.56 ms
       (cap):   3,249.39 ms
       setup:   5,206.04 ms
  (typeflow):  15,378.97 ms
   (objects):   6,795.22 ms
  (features):     152.68 ms
    analysis:  22,692.12 ms
    universe:   1,248.48 ms
     (parse):   6,206.26 ms
    (inline):   3,647.32 ms
   (compile):  27,832.51 ms
     compile:  39,458.58 ms
       image:   3,222.35 ms
       write:     646.89 ms
     [total]:  73,892.84 ms

ALL小文字の「hellograaljava」というバイナリが生成されました。
timeコマンド付けつつ実行してみた。なるほど彡(゚)(゚)

#通常のjava実行
time java HelloGraalJava
Hello, Graal Java!

real    0m0.187s
user    0m0.140s
sys     0m0.043s

#Native Image実行
time ./hellograaljava
Hello, Graal Java!

real    0m0.017s
user    0m0.003s
sys     0m0.011s

native-imageコマンドはpython関連のオプションも有るんですが、
使い方はイマイチ解らず。今後要検証彡(゚)(゚)

native-image --help
:
Available macro-options are:
    --language:python
:
8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4