Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ about coroutines

about coroutines

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Kevnuke   USA  (145 posts)  Bio
Date Mon 05 Aug 2019 08:27 PM (UTC)
Message
I've been reading about coroutines in the Lua 5.1 docs and had a few questions about it.

1) Is a coroutine related to multithreading or spawning a separate process? In other words, does it enable the use of multiple cores to allow the main process to happen on one core, and another process to happen in parallel, on a different core?

2) Do you recommend using them routinely instead of state variables? Pause a script with yield and resume it later when a condition is met, rather than repeatedly using an if statement to check the value of a variable (and wasting processor time as a result)?

3) What else are coroutines commonly used for, or are they a niche feature of the language?
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #1 on Mon 05 Aug 2019 10:17 PM (UTC)
Message

  1. No. They are just co-operative multitasking. It is a way of pausing a script and keeping its state.

  2. You have to have some reason to resume the script (and therefore the coroutine). Whatever that reason is, you will have to test for it, so you don't really get much further ahead by pausing a script rather than starting a new one.

  3. They are useful in situations where there might be pauses, as illustrated here: http://www.gammon.com.au/forum/?id=4957 and here: http://www.gammon.com.au/forum/?id=4956

    That lets you have a simple loop in a script, for example gathering lines of text from an inventory and building them into a table. The coroutine used there in the "wait" module just simplifies the code, it isn't really there to save time.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #2 on Mon 05 Aug 2019 11:33 PM (UTC)
Message
Alright, I know there's an example in PIL 9.4 using luasocket and coroutines to download multiple files simultaneously, but it's using HTTP instead of HTTPS. There's an API for characters in Achaea at https://api.achaea.com/characters.json that lists every online character's name and a url, such as https://api.achaea.com/characters/aegoth.json. You can follow each link to see basic info about that character.

I wanted to have a Lua table which stored that information for each character but I don't know how to go about it. Do I have to download and save the files before working with the data in them or can I do it in memory? I'd rather not have a local file for every logged-in character just to populate a Lua table with that data. I guess I'm looking for the simplest way to access the object at each url. So in the end I'd have this for each character:

player_data = {}
player_data.aegoth = {
    name          = "Aegoth",
    fullname      = "Holocaust King Aegoth Aristata, the Commandant of Baelgrim",
    city          = "mhaldor",
    house         = "legates",
    level         = "116",
    class         = "magi",
    mob_kills     = "116k",
    player_kills  = "7996",
    xp_rank       = "24",
    explorer_rank = "8"
}

Maybe something like what I use to get at GMCP json objects?

temp = json.decode(content)
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #3 on Tue 06 Aug 2019 04:42 PM (UTC)

Amended on Tue 06 Aug 2019 04:43 PM (UTC) by Fiendish

Message
LuaSocket can't do HTTPS and instead just silently shunts you to HTTP ( https://www.gammon.com.au/forum/?id=12520&reply=4 )

I find coroutines hard to use, so in the aardwolf mushclient package I use lua-llthreads2 and lua-openssl (because luasec isn't thread-safe) for non-blocking https requests with actual OS threads.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #4 on Tue 06 Aug 2019 08:21 PM (UTC)
Message
You could use a coroutine to manage a non-blocking HTTP request, but if it isn't thread-safe then you probably should not do more than one at a time.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #5 on Tue 06 Aug 2019 08:35 PM (UTC)

Amended on Wed 07 Aug 2019 02:50 AM (UTC) by Fiendish

Message
Quote:
You could use a coroutine to manage a non-blocking HTTP request


Plain HTTP is no longer sufficient, because many services (like this one) block non-TLS connections. Luasocket on its own won't work for the given URL because the server forces TLS.

Quote:
but if it isn't thread-safe then you probably should not do more than one at a time.


Luasec isn't safe, but Lua-openssl is and has an identical interface.

https://github.com/fiendish/aardwolfclientpackage
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


16,855 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.