This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
tfoote's profile picture

fs read and write at same time

Started by tfoote, 24 June 2012 - 11:55 PM
tfoote #1
Posted 25 June 2012 - 01:55 AM
is the fs file system can you read and write a file at the same time? I would use it for my wireless system… And it would use the parallel api
Bossman201 #2
Posted 25 June 2012 - 01:59 AM
Why would you need to re-store data you already have in memory or store data that you're about to overwrite?
MysticT #3
Posted 25 June 2012 - 02:00 AM
No, you can't read and write a file at the same time. You have to open it to read, then close it and open it to write.

Why would you need to re-store data you already have in memory or store data that you're about to overwrite?
Well, reading the file and then writing does make sense. You can read it, modify something and rewrite it.
tfoote #4
Posted 25 June 2012 - 02:52 AM
Well i need it for my server system. I will have multuple computers feeding into something. The code will basically just keep on going down the lines of code that come in… and it will re-broadcast it.
kazagistar #5
Posted 25 June 2012 - 06:39 AM
Why don't you just rebroadcast as soon as you get each message, and just not use the parallel api in the first place?


local event, id, message, distance
while true do
  event, id, message, distance = os.pullEvent("rednet_message")
  if correct(message) then
    processAndBroadcast(message)
  end
end
tfoote #6
Posted 25 June 2012 - 03:03 PM
That is what i planned on doing. The problem is i wanted to make it sort of like an ethernet cable. I wanted it to take some time to get there without using the sleep(). In case you havent noticed. I like to make it hard on myself. I should probably just take the easy way out…
kazagistar #7
Posted 25 June 2012 - 04:43 PM
In that case you will need to use a buffer. But the parallel API… it is overkill for something like this. Often, when people think they want threading, what they really want is event based programming. (Note: I haven't actually tried the code, but t should get the idea across).

local DELAY = 0.5
local event
local buffer = {}
while true do
	-- store the results of pullevent into a table
	event = {os.pullEvent("rednet_message")}
	-- handle redstone messages
	if event[1] == "rednet_message" then
		-- make sure the message is valid (however you want to)
		if valid(event) then
			-- store the message, indexed by the timer ID
			buffer[os.startTimer(DELAY)] = event[3]
		end
	-- handle timers
	elseif event[1] == "timer" then
		-- send the delayed message
		broadcast(buffer[event[2]])
	end
end