You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
---**This API is still experimental and may change in the future without notice.**
4255
+
---
4256
+
---**This API is available only for Linux.**
4257
+
---
4258
+
---Wrap the [nginx worker thread](http://nginx.org/en/docs/dev/development_guide.html#threads) to execute lua function. The caller coroutine would yield until the function returns.
4259
+
---
4260
+
---Only the following ngx_lua APIs could be used in `function_name` function of the `module` module:
4261
+
---
4262
+
---* `ngx.encode_base64`
4263
+
---* `ngx.decode_base64`
4264
+
---
4265
+
---* `ngx.hmac_sha1`
4266
+
---* `ngx.encode_args`
4267
+
---* `ngx.decode_args`
4268
+
---* `ngx.quote_sql_str`
4269
+
---
4270
+
---* `ngx.re.match`
4271
+
---* `ngx.re.find`
4272
+
---* `ngx.re.gmatch`
4273
+
---* `ngx.re.sub`
4274
+
---* `ngx.re.gsub`
4275
+
---
4276
+
---* `ngx.crc32_short`
4277
+
---* `ngx.crc32_long`
4278
+
---* `ngx.hmac_sha1`
4279
+
---* `ngx.md5_bin`
4280
+
---* `ngx.md5`
4281
+
---
4282
+
---* `ngx.config.subsystem`
4283
+
---* `ngx.config.debug`
4284
+
---* `ngx.config.prefix`
4285
+
---* `ngx.config.nginx_version`
4286
+
---* `ngx.config.nginx_configure`
4287
+
---* `ngx.config.ngx_lua_version`
4288
+
---
4289
+
---
4290
+
---The first argument `threadpool` specifies the Nginx thread pool name defined by [thread_pool](https://nginx.org/en/docs/ngx_core_module.html#thread_pool).
4291
+
---
4292
+
---The second argument `module_name` specifies the lua module name to execute in the worker thread, which would return a lua table. The module must be inside the package path, e.g.
4293
+
---
4294
+
---```nginx
4295
+
---
4296
+
---lua_package_path '/opt/openresty/?.lua;;';
4297
+
---```
4298
+
---
4299
+
---The third argument `func_name` specifies the function field in the module table as the second argument.
4300
+
---
4301
+
---The type of `arg`s must be one of type below:
4302
+
---
4303
+
---* boolean
4304
+
---* number
4305
+
---* string
4306
+
---* nil
4307
+
---* table (the table may be recursive, and contains members of types above.)
4308
+
---
4309
+
---The `ok` is in boolean type, which indicate the C land error (failed to get thread from thread pool, pcall the module function failed, .etc). If `ok` is `false`, the `res1` is the error string.
4310
+
---
4311
+
---The return values (res1, ...) are returned by invocation of the module function. Normally, the `res1` should be in boolean type, so that the caller could inspect the error.
4312
+
---
4313
+
---This API is useful when you need to execute the below types of tasks:
4314
+
---
4315
+
---* CPU bound task, e.g. do md5 calculation
4316
+
---* File I/O task
4317
+
---* Call `os.execute()` or blocking C API via `ffi`
4318
+
---* Call external Lua library not based on cosocket or nginx
4319
+
---
4320
+
---Example1: do md5 calculation.
4321
+
---
4322
+
---```nginx
4323
+
---
4324
+
---location /calc_md5 {
4325
+
--- default_type 'text/plain';
4326
+
---
4327
+
--- content_by_lua_block {
4328
+
--- local ok, md5_or_err = ngx.run_worker_thread("testpool", "md5", "md5")
4329
+
--- ngx.say(ok, " : ", md5_or_err)
4330
+
--- }
4331
+
--- }
4332
+
---```
4333
+
---
4334
+
---`md5.lua`
4335
+
---
4336
+
---```lua
4337
+
---local function md5()
4338
+
--- return ngx.md5("hello")
4339
+
---end
4340
+
---```
4341
+
---
4342
+
---Example2: write logs into the log file.
4343
+
---
4344
+
---```nginx
4345
+
---
4346
+
---location /write_log_file {
4347
+
--- default_type 'text/plain';
4348
+
---
4349
+
--- content_by_lua_block {
4350
+
--- local ok, err = ngx.run_worker_thread("testpool", "write_log_file", "log", ngx.var.arg_str)
4351
+
--- if not ok then
4352
+
--- ngx.say(ok, " : ", err)
4353
+
--- return
4354
+
--- end
4355
+
--- ngx.say(ok)
4356
+
--- }
4357
+
--- }
4358
+
---```
4359
+
---
4360
+
---`write_log_file.lua`
4361
+
---
4362
+
---```lua
4363
+
---
4364
+
--- local function log(str)
4365
+
--- local file, err = io.open("/tmp/tmp.log", "a")
0 commit comments