在Mac OS X上使用带有NLTK的Celery线程中的python-javabridge JVM导致致命错误

时间:2015-04-21 09:14:57

标签: python celery weka java-bridge-method

我正在使用基于Python wrapper for Wekapython-javabridge。我有很长的任务要执行,因此,我使用Celery来执行此操作。问题是我得到了

A fatal error has been detected by the Java Runtime Environment:

  SIGSEGV (0xb) at pc=0x00007fff91a3c16f, pid=11698, tid=3587

JRE version:  (8.0_31-b13) (build )
Java VM: Java HotSpot(TM) 64-Bit Server VM (25.31-b07 mixed mode bsd-amd64 compressed oops)
Problematic frame:
C  [libdispatch.dylib+0x616f]  _dispatch_async_f_slow+0x18b

Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again

If you would like to submit a bug report, please visit:
    http://bugreport.java.com/bugreport/crash.jsp
The crash happened outside the Java Virtual Machine in native code.
See problematic frame for where to report the bug.

在线程内部启动JVM时。这两行代码用于执行此操作(来自weka.core.jvm):

javabridge.start_vm(run_headless=True)
javabridge.attach()

据我所知,这可能是因为JVM没有连接到Celery线程。但是,javabridge.attach()确实在其中运行。

我错过了什么?


编辑:我发现了导致问题的代码。它与NLTK标记器有关。以下代码(根据Vebjorn's answer)将重现错误:

# hello.py
from nltk.tokenize import RegexpTokenizer
import javabridge
from celery import Celery

app = Celery('hello', broker='amqp://guest@localhost//', backend='amqp')

started = False    

@app.task
def hello():
    global started
    if not started:
        print 'Starting the VM'
        javabridge.start_vm(run_headless=True)
        started = True

    sentence = "This is a sentence with some numbers like 1, 2 or and some weird symbols like @, $ or ! :)"
    tokenizer = RegexpTokenizer(r'\w+')
    tokenized_sentence = tokenizer.tokenize(sentence.lower())
    print "Tokens:", tokenized_sentence

    return javabridge.run_script('java.lang.String.format("Hello, %s!", greetee);',
                             dict(greetee='world'))

在不启动JVM的情况下,代码可以正常运行。当作为Celery任务运行时,它也有效。我不明白为什么它会崩溃。


编辑2:它实际上适用于干净的Ubuntu环境(Dockerized),但不适用于Mac OS X Yosemite(v10.3)。


编辑3:正如评论中所提到的,如果from nltk.tokenize import RegexpTokenizer在任务包装器内完成,那就在hello()函数内部。

1 个答案:

答案 0 :(得分:1)

默认情况下,Celery启动四个单独的工作进程。 (请参阅-c的{​​{1}}命令行选项。)您需要确保在所有这些选项中启动JVM。这个例子对我有用:

celery worker

# hello.py
import os
import threading
from celery import Celery
import javabridge

app = Celery('hello', broker='amqp://guest@localhost//', backend='amqp')

started = False

@app.task
def hello():
    global started
    if not started:
        print 'Starting the VM'
        javabridge.start_vm(run_headless=True)
        started = True
    return javabridge.run_script('java.lang.String.format("Hello, %s!", greetee);',
                                 dict(greetee='world'))
  1. 在原始Ubuntu 14.04计算机上安装:

    # client.py
    from hello import hello
    
    r = hello.delay()
    print r.get(timeout=1)
    
  2. 启动工作人员:

    $ sudo apt-get update -y
    $ sudo apt-get install -y openjdk-7-jdk python-pip python-numpy python-dev rabbitmq-server
    $ sudo pip install celery javabridge
    $ sudo /etc/init.d/rabbitmq-server start
    
  3. 在另一个窗口中,运行五次客户端:

    $ celery -A hello worker
    ...
     -------------- celery@a7cc1bedc40d v3.1.17 (Cipater)
    ---- **** ----- 
    --- * ***  * -- Linux-3.16.7-tinycore64-x86_64-with-Ubuntu-14.04-trusty
    -- * - **** --- 
    - ** ---------- [config]
    - ** ---------- .> app:         hello:0x7f5464766b50
    - ** ---------- .> transport:   amqp://guest:**@localhost:5672//
    - ** ---------- .> results:     amqp
    - *** --- * --- .> concurrency: 4 (prefork)
    -- ******* ---- 
    --- ***** ----- [queues]
     -------------- .> celery           exchange=celery(direct) key=celery
    
    
    [2015-04-21 10:04:31,262: WARNING/MainProcess] celery@a7cc1bedc40d ready.
    
  4. 在工作窗口中观察JVM是从客户端的前四次调用(进入四个差异进程)而不是第五次调用启动的:

     $ python client.py 
     Hello, world!
     $ python client.py 
     Hello, world!
     $ python client.py 
     Hello, world!
     $ python client.py 
     Hello, world!
     $ python client.py 
     Hello, world!