• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

xopxe/lumen: Lua Multitasking Environment.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

xopxe/lumen

开源软件地址(OpenSource Url):

https://github.com/xopxe/lumen

开源编程语言(OpenSource Language):

Lua 98.3%

开源软件介绍(OpenSource Introduction):

Lumen: Lua Multitasking Environment.

"A nice generic framework to develop complex, portable concurrent applications in Lua."

Introduction

Lumen is a very simple environment for coroutine based multitasking. Consists of a scheduler, and that's it. The API was inspired by a brief description of Sierra's scheduler. Lumen has no external dependencies nor C code, and runs on unmodified Lua (works with Lua 5.1, 5.2 and LuaJIT). Tasks that interface with LuaSocket and nixio for socket and async file I/O support are provided.

Lumen's API reference is available in the docs/ directory, or online.

How does it look?

Here is a small program, with two tasks: one emits ten numbered signals, one second apart. Another tasks receives those signals and prints them.

    local sched=require 'lumen.sched'

    -- task receives signals
    sched.run(function()
    	local waitd = {'an_event'}
    	while true do
    		local _, data = sched.wait(waitd)
    		print(data)
    	end
    end)
    
    -- task emits signals
    sched.run(function()
    	for i = 1, 10 do
    		sched.signal('an_event', i)
    		sched.sleep(1)
    	end
    end)
        
    sched.loop()

Tasks

Tasks can emit signals, and block waiting for them, and that's it.

  • A signal can be of any type, and carry any parameters
  • A task can wait on several signals, with an optional timeout.
  • Signals can be buffered; this helps avoid losing signals when waiting signals in a loop.
  • There is an catalog that can be used to simplify sharing data between tasks.

Pipes & Streams

There are also pipes and streams, for intertask communications.

  • Unlike with plain signals, writers can get blocked too (when pipe or stream gets full).
  • Synchronous and asynchronous (with a timeout) modes supported.
  • Multiple readers and writers supported.
  • For when no signal can get lost!

Mutexes

There are cases when you must guarantee that only one task is accessing a piece of code at a time. Mutexes provide a mechanism for that. Notice that Lumen, being a cooperative scheduler, will never preempt control from a task. That means you only may have to resort to mutexes when your critical piece of code relinquish control explicitly, for example with a call to sleep, emitting a signal or blocking waiting for a signal.

Goodies

There are a few other useful modules, like an integrated remote Lua shell and a lightweigth HTTP server.

How to try it out?

There several test programs in the tests/ folder. This example has a few tasks exchanging messages, showing off basic functionality:

lua test.lua

License

Same as Lua, see COPYRIGHT.

Who?

Copyright (C) 2012 Jorge Visca, [email protected]

Contributors

Andrew Starks (@andrewstarks)




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap