Page 1 of 2

Threads not working?

Posted: Mon Dec 14, 2015 10:44 pm
by rod_sn
I wanted to make a http request while my game is running, and i tought the threads would be the perfect way to do it. However i found no snippets on the wiki and i found few up-to-date examples (which i tried to use but still can't make it to work). So far i have this 2 files:

main.lua

Code: Select all

function love.load()
  http = require("socket.http")
   thread = love.thread.newThread("thread.lua")
   channel = love.thread.getChannel("test")
   thread:start()
   i = {}
  channel:supply("get")
end
function love.update(dt)
  
    v = ""
     v = channel:pop()
    print(v)
end
function love.draw()
   --love.graphics.print(tostring(i[1]), 10, 10)
end

function love.keypressed(key, isrepeat)
channel:supply("get")
    v = ""
     v = channel:pop()
    print(v)
end
thread.lua

Code: Select all

c = love.thread.getChannel("test")
http = require("socket.http")

num = c:pop()
print(num)
if num == "get" then
  print("staring")
  res = http.request("http://google.com")
  --print(res)
  c:supply(res)
end
I wanted to test using the love.keypressed to send a http request but only the channel:supply("get") on the love.load works, whenever i press a key the program stops responding. What am i doing wrong? Is there another way to get http requests without freezing my game while waiting for a response?

Re: Threads not working?

Posted: Tue Dec 15, 2015 3:00 am
by pgimeno
pop() does not wait, but supply() does wait. I think what's happening is:

1. Your thread starts and does a pop(). Since there's nothing, it returns nil, executes the rest of the code, and the thread terminates.
2. When you press a key, the main application uses supply(), which waits for someone to receive it. But there's no one, because the thread has terminated.

An immediate solution is to use demand() instead of pop() in the thread, which does wait until there's data.

Similarly, use push() rather than supply() in main, so that the main application is not blocked until the thread can attend it.

Re: Threads not working?

Posted: Tue Dec 15, 2015 3:26 am
by bobbyjones
I have done the same thing 2 months ago in one of my projects. I recommend using bartbes' async module. An example of its use can be found here.
https://github.com/Bobbyjoness/To-Do-App-Client
his asynce module can be found here https://github.com/bartbes/love-misc-li ... ster/async

Re: Threads not working?

Posted: Tue Dec 15, 2015 1:08 pm
by s-ol
The Problem is that your thread exits after doing one request, you need an (infinite) loop there.
also it still wouldn't work as expected because as pgimeno pointed out, pop doesn't wait for a value to become available so you might "miss" the result.

Take a look at the wiki and look at the differences between "pop" and "demand" and "push" and "supply".

Re: Threads not working?

Posted: Sun Dec 20, 2015 10:01 pm
by rod_sn
So... which is the best way of making http requests without lagging the game?

Re: Threads not working?

Posted: Sun Dec 20, 2015 10:10 pm
by s-ol
rod_sn wrote:So... which is the best way of making http requests without lagging the game?
Well, threads are. You just have to use the right operations to get the data through the channels.

Re: Threads not working?

Posted: Thu Dec 31, 2015 3:40 am
by rod_sn
This is so complicated ;-; Can someone just make a quick demo? Just download google page in a seperate thread. I feel so confused :cry:

Re: Threads not working?

Posted: Fri Jan 01, 2016 4:11 am
by player_258
rod_sn wrote:This is so complicated ;-; Can someone just make a quick demo? Just download google page in a seperate thread. I feel so confused :cry:
write http://love2d.org in there, it'll give you the html code using threads,
for some reason, http://google.com gives a utf8 error for me.

Re: Threads not working?

Posted: Sat Jan 02, 2016 7:29 pm
by rod_sn
Thank you... i didnt knew i needed a while loop on the thred file... Thanks a lot! Btw how can i keep track of different responses at the same time? I mean, the result variable will return the latest response.. Any ideas?

Re: Threads not working?

Posted: Sat Jan 02, 2016 9:41 pm
by player_258
Channel:getCount()
you can get them all at once, and put them in a table or something

Code: Select all

function getEvents(myChannel)
	local events = {}
	for i = 1, myChannel:getCount() do
		table.insert(events, myChannel:pop())
	end
	return events
end
it's up to you deciding what to do in case of getting +1 results.

btw, you should take "S0lll0s" advice,
Take a look at the wiki and look at the differences between "pop" and "demand" and "push" and "supply".