How to make sure to not miss event [closed] - multithreading

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Say I have a program that does the following:
wait for key press
Once key pressed, do complex query (takes 10 seconds) and print the result
repeat
Now if I my key presses are 10 seconds apart, this would not be a problem. How do I handle key presses really close together. Even worse, keys pressed at the exact same time.
Is this information bound to be lost?
I know threading might be an option but what is general way of doing this?
Is it possible to just store every key pressed even though other code is running and be able to tend to it later?

Interrupts. Universally, computers provide a mechanism for peripherals to request the attention of a CPU by asserting an Interrupt Request Signal. The CPU, when it is ready to accept interrupts, responds by saving minimal state somewhere, then executing code which can query the peripheral, possibly accepting data (keypress) from it.
If you are using an OS this is all hidden by the kernel, which typically exposes some mechanisms for you to choose how you want to deal with it:
Queue up the keypresses and process them later. Thus if I want to have query 1,3,5 in that order, I can press those keys in succession and go for a smoke while your long processing occurs.
Discard the lookahead keypresses; thus demand the user interact with a lousy UI. Search for "homer simpson work from home" to see how to work around this.
If you are using an OS, you might need to look up various ioctl's to enable this behaviour, use a UI packages similar to curses, or other.
If you aren't using an OS, your job is both tougher and easier: you have to write the code to talk to the keyboard, but implementing the policy is 1/10 th work of figuring out some baroque UI library.

Related

How to send image as a REST API response? What guidelines to follow? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I am making an application using Node.js and Express. I am able to save files to the server using multer, however while sending responses I have two options,
Send the URI of the image in JSON, and let the front-end call this to display the image.
Send the image data using some form of encoding like Base64 as part of JSON.
Since I am new to web development, I am confused as to which option to use. Some people have said that the first option requires two API calls, so can be slow. While I have also heard that the second option will take up more memory resources.
What other things should I consider while choosing, and is there any other way of sending images to the client side?
Option 1
Is less complex since no conversion is needed. These 2 API calls won't slow you down. The image size is way more important!!.. The file can be stored/accessed directly on filesystem and served from there. Also a filedownload is implemented in a short period of time. Also the base64 encoding makes the file roughly ~33% (!!) bigger what has a huge impact on large files regarding performance.
Option 2
Base 64 is more secure as nobody can link to your website as described here .
You only need to use base64 for security reasons OR if you have to transfer the image data as string if you cannot transfer it as binary.
Use Case
If this is your private non-productive project just try out both and use the one you like. In the end you are learning something.. It's only important to stay consistent !
If one option fits better to you, just implement it the way you like. You can always refactor a given part of the application later when you may have more experience or when the core parts of your application are finished. Sometimes, after working a while with one of the techniques it gets more clear which approach to use.
For learning it's sometimes better to go ahead, and implement something what works
and start to refactor as problems occur. Rather than overengineering small

How can a system like Tesla’s AutoPilot keep up with constant changing requests for multiple process? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
As a software developer, I am trying to understand how a system could possibly work as fast and efficiently enough and operate with consistanly and flawlessly with such precision for all the ongoing actions it needs to account for in a system such as a Tesla AutoPilot (self driving car system)...
In a car driving driving 65 MPH, if a deer runs out in front of the car, it immediately makes adjustments to protect the vehicle from a crash - while having to keep up with all the other sensors requests constantly firing off at the same time for possible actions on a micro-milllisecond, without skipping a beat.
How is all of that accomplished sysinctly? And have processing reporting back to it so quickly that it almost intentaniously is able to respond (without getting backed up with requests)?
I don't know anything about Tesla code, but I have read other real time code and analysed time slips in it. One basic idea is that if you check something every millisecond you will always respond to change within a millisecond. The simplest possible real time system has a "cyclic executive" built around a repeating schedule that tells it what to do when, worked out so that in all possible cases everything that has to be dealt with is dealt with within its deadline. Traditionally you are worrying about cpu time here, but not necessarily. The system I looked at was most affected by the schedule for a serial bus called a 1553 (https://en.wikipedia.org/wiki/MIL-STD-1553)- there almost wasn't enough time to get everything transmitted and received on time.
This is a bit too simple because it doesn't cope with rare events which have to be dealt with really quickly, such as response to interrupts. Clever schemes for interrupt handling don't have as much of an advantage as you would expect, because there is often a rare worst case that makes the clever scheme underperform a cyclic executive and real time code has to work in the worst case, but in practice you do need something with interrupt handlers and high priority processes that must be run on demand and with low priority processes that can be ignored when other stuff needs to make deadlines but will be run otherwise. There are various schemes and methodologies for arguing that these more complex systems will always make their deadlines. One of the best known ones is https://en.wikipedia.org/wiki/Rate-monotonic_scheduling. See also https://en.wikipedia.org/wiki/Earliest_deadline_first_scheduling.
An open source real time operating system that has seen real life use is https://en.wikipedia.org/wiki/RTEMS.

real time online push button based counting system [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am doing this project:
I have 4 inputs. These are push buttons, connected to a microcontroller.
Each time a push button is pressed, say for example pushbutton_1 is pressed, the press of a switch button should be recognised as a HIGH.
In its normal unpressed state it should be recognised as a LOW.
Then using serial communication i should transfer it to a computer.
Along with this, i need to implement a count for each button.
Each time a push button is pressed, the count that is assigned to that push button, should increment by 1.
The data arriving through serial communication should be transferred to an excel sheet/database.
The excel sheet/database should display a count for each pushbutton.
I have 4 important question areas:
Which microcontroller should i use? (I have experience with arduino development platform)
How do i implement the transfer of data from microcontroller to computer via serial communication?
Afterwards, how do i transfer the arriving data to MS excel/database?
How do i run implement the system in realtime?
Please suggest me the best possible way to implement this system.
To solve this using an MPU like an RPi via the Internet, its pretty trivial. To do this:
Wire your switches to the GPIO inputs on the Pi. This is a trivial example: http://razzpisampler.oreilly.com/ch07.html
When the state changes, send a message via a realtime service such as PubNub (free for student and other uses: http://www.pubnub.com/free-evangelism-program/)
On a remote "server-side", take the data received via the subscriber logic write to a CSV.
If you followed these directions, you would use the PubNub python client to publish the data from the Pi: https://github.com/pubnub/python/tree/master/python#publish
and then you would use python (PubNub supports over 70 languages, so you could use python or the language of your choice) to subscribe to the pushbutton data channel(s)
https://github.com/pubnub/python/tree/master/python#subscribe
You could even make a cool realtime updating web page in HTML/JS using the PubNub JS client
Source: https://github.com/pubnub/javascript/tree/master/web
Docs: http://www.pubnub.com/docs/javascript/api/reference.html#subscribe
to dynamically update a dashboard, with no file writing needed.

"Seamless" multi user session in linux/X11 [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
The goal
I would love to have a multi user system (based on linux) using only one X11 session with multiple screens and pairs of mouse and keyboard. So two (or more) people can work with the same computer sharing not only the same hardware but also the same "screen" (which would be split into two physical screens of course, but you could move a window to your partner for example...). Sharing the windows should not only make it more convenient to "show" your partner what you have done - if user A started to work on something using a complex application (assert that it wouldn't be convenient to save the files and open them in the other session) moving the window of the application to user B should be as simple as moving a window within your own screen. That's why I call it a "seamless" multi user session.
Possible solutions
I read about X11 "multi seat" in this article which doesn't have the features that I want. It uses a session for each user rather than one single session.
I found XI2 aka Xinput2 which provides a multi-pointer support. This allows having two separate mouse pointers controlled by two mice. I read that you can assign two keyboards to the two mice providing independant focus and text input. But I wonder if the clipboards (both "real" and "middle mouse button" clipboards) are treated separately too... I found only few information on XI2 multi pointer feature but no "field report".
Another, completely different idea would be having two separate X11 sessions on the computer but share the windows using X11-forward between the two sessions. BUT: As far as I know, you can not share a X11-forwarded window so that user A runs an application and while it runs, send the window to user B. As I know, only user B can run an application on the hardware of user A and display the window on it's own X11 session. That's again not what I want... Or am I wrong and it is possible to forward a window via X11-forwarding AFTER the application has been started?
edit: I just found XPRA which is similar to X11 forwarding but allows detaching and attaching a running application from / to an X11 session. I give it a try now.
Any other ideas to get this done?
I think I found a solution:
Win Switch (uses Xpra, licenced under GPL3)

Easy-to-use AutoHotkey/AutoIt alternatives for Linux [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I'm looking for recommendations for an easy-to-use GUI automation/macro platform for Linux.
If you're familiar with AutoHotkey or AutoIt on Windows, then you know exactly the kind of features I need, with the level of complexity. If you aren't familiar, then here's a small code snippet of how easy it is to use AutoHotkey:
InputBox, varInput, Please enter some random text...
Run, notepad.exe
WinWaitActive, Untitled - Notepad
SendInput, %varInput%
SendInput, !f{Up}{Enter}{Enter}
WinWaitActive, Save
SendInput, SomeRandomFile{Enter}
MsgBox, Your text`, %varInput% has been saved using notepad!
#n::Run, notepad.exe
Now the above example, although a bit pointless, is a demo of the sort of functionality and simplicity I'm looking for. Here's an explanation for those who don't speak AutoHotkey:
----Start of Explanation of Code ----
Asks user to input some text and stores it in varInput
Runs notepad.exe
Waits till window exists and is active
Sends the contents of varInput as a series of keystrokes
Sends keystrokes to go to File -> Exit
Waits till the "Save" window is active
Sends some more keystrokes
Shows a Message Box with some text and the contents of a variable
Registers a hotkey, Win+N, which when pressed executes notepad.exe
----End of Explanation----
So as you can understand, the features are quite obvious: Ability to easily simulate keyboard and mouse functions, read input, process and display output, execute programs, manipulate windows, register hotkeys, etc. - all being done without requiring any #includes, unnecessary brackets, class declarations, etc. In short: Simple.
Now I've played around a bit with Perl and Python, but it's definitely not AutoHotkey. They're great for more advanced stuff, but surely, there has to be some tool out there for easy GUI automation, right?
PS: I've already tried running AutoHotkey with Wine, but sending keystrokes and hotkeys don't work.
I'd recommend the site alternativeto.net to find alternative programs.
It shows three alternatives for AutoIt: AutoKey, Sikuli, and Silktest. AutoKey seems up to the job.
IronAHK is being developed as a cross-platform flavor of AutoHotkey which can be used on Linux, but it's not a fleshed out product yet.
Sikuli lets you automate your interface using screenshots. It runs on any Java platform, so it is cross-platform.
You should look at Experitest. I'm using the Windows version, but it's Java-based and I think it supports Linux as well.

Resources