I use three different threads to read can messages from socket CAN raw and to write can messages to another socket CAN raw, The message read every 2 seconds are put onto queue and retrieved from queue on another thread, mutex thread functions are used, The idea is to write the CAN message every 2 seconds as read. But CAN messages are written every 0.3 millisecond by constantly retrieving from the queue. when the message is only read and put onto queue on every 2 second, so queue the message that is retrieved is not getting removed and remains stagnant in the queue. Every time I try to increase the msgsize, the program shows segmentation fault or automatically killed by the OS. How do i go about debugging this issue. please help, thank you
I have a wxApp implementation in which the wxFrame embeds a CEF browser.I have bindings from CEF framework to perform some Javascript bindings. The application shows up the initial screen and loads everything good. However, after a certain delay (not consistent), the app frame becomes non-responsive and does not handle any of the events. I have put up a wxTimer to figure out the amount of time it takes and it varies between 28 seconds to 60 seconds. This is where the timer events are also not called. Now when I press on the x or maximize icon on the window, all the pending events are sent and the application becomes active again.
Here are the options I tried:
Called wxTheApp->ProcessPendingEvents(); on the timer (runs every 1 second)
Tried and looked into onIdle method. This is also getting paused after a while
My guess is that the application main thread is paused for some reason and is not able to revert back unless any of the UI actions are triggering it.
The textbook way to read a file line-by-line in NodeJS seems to be to call readline.createInterface, and then afterward attach event handlers for line and close.
There doesn't seem to be anything to "start" the reader. It just goes, and seems to work perfectly. How does it know when to start reading? How does it guarantee that those events, which don't exist yet, will always pick up every line in the file?
I always assumed that it just all happened so fast that the events get attached faster than it takes to open the file from disk and start reading it - but that doesn't really hold up.
For example, suppose I put some heavy CPU-consuming code after the lineReader has been created, but before the events attached. It still seems to work, and the event still fires for each line. How did it "wait" until the heavy stuff was done before it started reading? If I don't attach the line event, then it runs anyway and the close event still fires, so it's not like it's waiting for the line event to be created.
var lineReader = readline.createInterface({
input: fs.createReadStream("input.txt")
});
// EVENTS HAVE NOT BEEN CREATED YET
lineReader.on("line", line => { console.log(line); });
lineReader.on("close", () => { console.log("DONE"); });
This isn't specific to lineReader - seems to be a common Node pattern - this is just the easiest to define and run.
Internally, readline.createInterface() is creating a stream. Streams, by default, are paused. They unpause themselves in a number of ways and what's relevant here is when a data event listener is added.
And, inside of readline.createInterface(), a data event handler is added. That starts the stream flowing and it will start emitting data events which the readline code will parse into line events.
Also because node.js and streams are event driven and node.js runs your Javascript as single threaded, that means that no events will occur until your setup code finishes executing. Internally, node.js may have already started reading the file (using asynchronous I/O and threads internally), but even if it finishes the first read from the file before your setup code finishes executing, all it will do is insert a data event in the event queue. node.js won't process that data event until your setup code is done executing and has returned control back to the node.js event loop.
Then, the data event callback will be called, the readline code will parse the data from that first event and if there is a full line in that first data event, it will then trigger a line event.
There doesn't seem to be anything to "start" the reader.
Attaching a data event handler on the readStream (internal to the readline code) is what tells the stream to start flowing.
It just goes, and seems to work perfectly. How does it know when to start reading?
Same as above.
How does it guarantee that those events, which don't exist yet, will always pick up every line in the file?
The readline code receives raw data from the file in its data event handler. It then parses that code into lines and emits line events for each line that it finds. When a file read crosses a line boundary, it must buffer a partial line and wait for the rest of the line to come on the next data event from the stream.
When the linereader code sees that the stream is done reading and there are no more bytes, it sends the last line (if there is one in the buffer) and then issues the close event to tell the listener that its all done.
For example, suppose I put some heavy CPU-consuming code after the lineReader has been created, but before the events attached. It still seems to work, and the event still fires for each line. How did it "wait" until the heavy stuff was done before it started reading?
This is because node.js is event-driven. The first data event from the stream (internal to the readline code) is the result of an fs.readFile() function and that notifies completion through the event queue. An event in the event queue will not be processed until the current piece of Javascript finishes and returns control back to the event loop (at which point it will then service the next event waiting in the event queue). So, no matter how much have CPU-consuming code you have before you attach the event handlers, the internals of readline won't be told about the first data read from the file until all that is done.
It is this single-threaded, event-driven nature that ensures that you get to install your event listeners before those events can be triggered so there's no way you can miss them.
If I don't attach the line event, then it runs anyway and the close event still fires, so it's not like it's waiting for the line event to be created.
Correct. The readline code attaches the data event handler inside the createInterface() call, whether you have a line event listener or not. So, the stream will start flowing and the file will get read whether you have a line event handler or not.
FYI, one way you can help answers these questions yourself is to just go look at the node.js code and see how it works. That's what I did here. Here's a link to the createInterface() function where you can see what I've described here.
And, you can see here in the stream doc, where is describes the three ways that a stream starts flowing, one of which is the attaching of a data event listener.
I am building a ambient audio skill for sleep for Alexa! I am trying to loop the audio so I don't have to download 10 hour versions of the audio. How do I get the audio to work? I have it build to where it will play the audio, but not loop.
I've solved this problem in my skill Rainmaker: https://www.amazon.com/Arif-Gebhardt-Rainmaker/dp/B079V11ZDM
The trick is to handle the PlaybackNearlyFinished event.
https://developer.amazon.com/de/docs/alexa-voice-service/audioplayer.html#playbacknearlyfinished
This event is fired shortly before the currently playing audio stream is ending.
Respond to the event with another audioPlayerPlay directive with behavior ENQUEUE. This will infinitely loop your audio until it gets interrupted by e.g. the AMAZON.StopIntent.
Advanced: if you want a finite loop, say ten times your audio, use the token of the audioPlayerPlay directive to count down from ten. Once the counter hits zero, just don't enqueue another audio. But be sure to respond something in this case, even if it's just an empty response. Otherwise you will get a timeout error or the like.
I have a playlist, and I want to sequentially play through the tracks, but every time a new track is loaded, I want to call a function. How would I go about listening for this event?
SPPlaybackManager, the playback class in CocoaLibSpotify, doesn't automatically play tracks sequentially, so you have to manually tell it to play each time. Since you're managing that, you already know when a new track is starting playback.
Additionally, SPPlaybackManagerDelegate has a method -playbackManagerWillStartPlayingAudio:, which will let you know when audio starts hitting the speakers.