当读取慢速外部URL时,带有协同程序的Vertx会阻塞

时间:2018-02-22 09:42:03

标签: kotlin vert.x vertx-verticle

我对Vert.x很陌生,并准备做一个小例子,当我得到GET - 请求到根路径时,每次从某个外部服务器读取当前时间。我以为使用launchawaitResult将为我处理异步部分,但日志告诉我,该线程正在阻塞。对外部URL的请求大约需要5到7秒。使用更快的网址是可能的,但我有意想使用慢速网页来感受Vert.x的异步方面。

有人可以帮助我吗?我如何处理协同程序?

class MainVerticle : CoroutineVerticle() {
    override suspend fun start() {
        val server = vertx.createHttpServer()
        val router = Router.router(vertx)

        router.get("/home").handler { handleGet(it) }

        server.requestHandler {
            router.accept(it)
        }.listen(8080) { }
    }

    private fun handleGet(event: RoutingContext) {
        launch(vertx.dispatcher()) {
            // this request takes about 5 secs the first time
            val url = "my-slow-url"

            val jsonStr = awaitResult<String> { handler ->
                val (_, response) = url.httpGet().responseString()
                val jsonStr = response.data.toString(Charsets.UTF_8)
                handler.handle(Future.succeededFuture(jsonStr))
            }
            ...
        }
    }
}

但那给了我

Feb 22, 2018 10:30:57 AM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-0,5,main] has been blocked for 2025 ms, time limit is 2000
Feb 22, 2018 10:30:58 AM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-0,5,main] has been blocked for 3025 ms, time limit is 2000
Feb 22, 2018 10:30:59 AM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-0,5,main] has been blocked for 4025 ms, time limit is 2000
Feb 22, 2018 10:31:00 AM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-0,5,main] has been blocked for 5026 ms, time limit is 2000
io.vertx.core.VertxException: Thread blocked

2 个答案:

答案 0 :(得分:0)

两个错误:

您的功能无法暂停:

private fun handleGet(event: RoutingContext) {

你在EventLoop上发送:

launch(vertx.dispatcher()) {

另外,我不确定你是如何实际获取你的网址的。我希望它是非阻塞的,然后你可以使用.await()

http://vertx.io/docs/vertx-lang-kotlin-coroutines/kotlin/#_awaiting_the_completion_of_vert_x_futures

答案 1 :(得分:0)

使用>>> df =pd.concat([df.drop('PreviousDRGs', 1), pd.get_dummies(df['PreviousDRGs'] .str.split(",", expand=True), prefix = 'PreviousDRGs').max(level=0, axis=1).asty pe(np.int8)], axis=1) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\decisionsupport\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\generic.py", line 11616, in stat_func return self._agg_by_level(name, axis=axis, level=level, skipna=skipna) File "C:\Users\decisionsupport\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\generic.py", line 10440, in _agg_by_level grouped = self.groupby(level=level, axis=axis, sort=False) File "C:\Users\decisionsupport\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\generic.py", line 7894, in groupby **kwargs File "C:\Users\decisionsupport\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\groupby\groupby.py", line 2522, in groupby return klass(obj, by, **kwds) File "C:\Users\decisionsupport\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\groupby\groupby.py", line 363, in __init__ obj._consolidate_inplace() File "C:\Users\decisionsupport\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\generic.py", line 5252, in _consolidate_inplace self._protect_consolidate(f) File "C:\Users\decisionsupport\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\generic.py", line 5241, in _protect_consolidate result = f() File "C:\Users\decisionsupport\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\generic.py", line 5250, in f self._data = self._data.consolidate() File "C:\Users\decisionsupport\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\managers.py", line 932, in consolidate bm._consolidate_inplace() File "C:\Users\decisionsupport\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\managers.py", line 937, in _consolidate_inplace self.blocks = tuple(_consolidate(self.blocks)) File "C:\Users\decisionsupport\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\managers.py", line 1913, in _consolidate list(group_blocks), dtype=dtype, _can_consolidate=_can_consolidate File "C:\Users\decisionsupport\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\blocks.py", line 3323, in _merge_blocks new_values = new_values[argsort] numpy.core._exceptions.MemoryError: Unable to allocate array with shape (4524, 1 38299) and data type uint8 >>> 阻止诸如URL提取之类的呼叫。

awaitBlocking是要获得一个处理程序,该处理程序可用作基于回调的函数(如事件总线api)的语法糖。因此,您无需传递回调,而是将从awaitResult获得的处理程序作为参数传递。也就是您的基础代码正在阻止-将其包装在事件循环中的awaitResult中仍然会导致事件循环被阻止