Coder Social home page Coder Social logo

lua-fiber's Introduction

lua-fiber

This is a sugar for coroutines extracted from Luvit

You pass a callback to fiber.new. The first arg to this callback is a function, usually called wrap. You can then call wrap to any function and it will be called in an asynchronous coroutine

Sample use

local fiber = require 'fiber'

local function logic(wrap)
  -- Wrap some functions for sync-style calling
  local sleep = wrap(require('timer').setTimeout)
  -- Can wrap modules too
  local fs = wrap(require('fs'), true) -- true means to auto-handle errors

  print("opening...")
  local fd = fs.open(__filename, "r", "0644")
  p("on_open", {fd=fd})

  print("fstatting...")
  local stat = fs.fstat(fd)
  p("stat", {stat=stat})

  print("reading...")
  local offset = 0
  repeat
    local chunk, length = fs.read(fd, offset, 40)
    p("on_read", {chunk=chunk, offset=offset, length=length})
    offset = offset + length
  until length == 0

  print("pausing...")
  sleep(1000)

  print("closing...")
  fs.close(fd)
  p("on_close", {})

  return fd, stat, offset

end

print "Starting fiber."
fiber.new(logic, function (err, fd, stat, offset)
  if err then
    p("ERROR", err)
    error(err)
  else
    p("SUCCESS", { fd = fd, stat = stat, offset = offset })
  end
end)
print "started."

print "Starting another fiber."
fiber.new(function (wrap)

  local readdir = wrap(require('fs').readdir)
  print("scanning directory...")
  local err, files = readdir(".")
  p("on_open", {err=err,files=files})

end)
print "started second."

lua-fiber's People

Contributors

jeduan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.