Threads not working?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
rod_sn
Prole
Posts: 27
Joined: Sun Jan 11, 2015 9:59 am

Threads not working?

Post 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?
Attachments
threads.love
(1.53 KiB) Downloaded 115 times
"You certainly usually find something, if you look, but it is not always quite the something you were after."
-Thorin Oakenshield, J.R.R. Tolkien
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: Threads not working?

Post 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.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Threads not working?

Post 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
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Threads not working?

Post 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".

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
rod_sn
Prole
Posts: 27
Joined: Sun Jan 11, 2015 9:59 am

Re: Threads not working?

Post by rod_sn »

So... which is the best way of making http requests without lagging the game?
"You certainly usually find something, if you look, but it is not always quite the something you were after."
-Thorin Oakenshield, J.R.R. Tolkien
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Threads not working?

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
rod_sn
Prole
Posts: 27
Joined: Sun Jan 11, 2015 9:59 am

Re: Threads not working?

Post 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:
"You certainly usually find something, if you look, but it is not always quite the something you were after."
-Thorin Oakenshield, J.R.R. Tolkien
User avatar
player_258
Prole
Posts: 9
Joined: Thu Jun 20, 2013 3:37 pm

Re: Threads not working?

Post 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.
Attachments
http.love
(882 Bytes) Downloaded 137 times
sorry if i make any english mistake, not my native language
User avatar
rod_sn
Prole
Posts: 27
Joined: Sun Jan 11, 2015 9:59 am

Re: Threads not working?

Post 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?
"You certainly usually find something, if you look, but it is not always quite the something you were after."
-Thorin Oakenshield, J.R.R. Tolkien
User avatar
player_258
Prole
Posts: 9
Joined: Thu Jun 20, 2013 3:37 pm

Re: Threads not working?

Post 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".
sorry if i make any english mistake, not my native language
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests