Skip to content. | Skip to navigation

Personal tools
Log in
Sections
You are here: Home

jorgenmodin.net - Blog

Terminating a process and its subprocesses from python

Posted by admin |

I'm testing a python server from a python test script. The server reads a config file on startup and I want to test that it actually works. So I want the script to test the server as if it were run from the command line. When run from the command line, the server can be terminated by typing Ctrl-c.

The server starts a child process from inside itself. Problem is, if I start the server with subprocess, and then send the kill/terminate signal to it via subprocess, the server's child process keeps running.

The trick is to assign a new process group to the server, separate from the testing script. In this way the os module can issue a "killpg" command that takes care of terminating the server and its child processes, without killing the testing script.

import subprocess
import os
import signal

p=subprocess.Popen(your_command, preexec_fn=os.setsid, shell=True) 
os.killpg(os.getpgid(p.pid), signal.SIGTERM) 



Found this solution on Stackoverflow:

To handle the general problem:
p=subprocess.Popen(your_command, preexec_fn=os.setsid) 
os.killpg(os.getpgid(p.pid), 15) 
setsid will run the program in a new session, thus assigning a new process group to it and its children. calling os.killpg on it thus won't bring down your own python process also.


Read more: Link - unix - Killing a subprocess including its children from python - Stack Overflow

May 22, 2015 12:45

Big wars may be in our future

Posted by admin |

It has been common to assume that man's lot in the world is getting better and better, and that one of the things contributing to this, is less and less death caused by war.

However if you note and measure that the distribution of deaths from war through history is seriously fat-tailed (which means that very rarely occuring wars do kill a whole lot of people), then almost all measurements you will take on the way will be below the true mean. And we may in fact be up for serious mayhem. Thsi is presented in a paper (liked at the end) by Nassim Nicholas Taleb and Pasquale Cirillo, and blogged about by Mark Buchanan here:

Violent warfare is on the wane, right? — Bull Market — Medium -

For example, it turns out that, for a process following this statistical pattern, one should expect fully 96% of all observations to fall below the true mean of the process. This brings home just how non-Gaussian and non-normal this process is. We’re used to thinking that, if we observe instances from some random process, we ought to (very crudely) see events about half above and half below the mean. Instead, in this process, one should expect that almost all observations will be below, and even far below, the actual mean. We almost always see fewer wars than we, in a sense, should.

Original paper: http://www.fooledbyrandomness.com/violence.pdf

May 18, 2015 03:20

Why devices interface to Android via the headset jack

Posted by admin |

It seems that more and more devices interface with Android phones and tablets via the headset jack. As examples here is a brand new geiger counter: http://www.kjell.com/sortiment/telefoni-kommunikation/mobiltelefon-tillbehor/gadgets/geiger-och-uv-matare/geigermatare-for-mobilen-p96153#ProductDetailedInformation

And here is the new credit card processing unit for iZettle (the older one uses Bluetooth). https://www.izettle.com/se/card-readers

I have used the IZettle bluetooth device with different devices and it just doesn't work on all devices, due to as far as i understand different bluetooth stacks.

I recently tried to interface to an Arduino with a USB library through an OTG . It worked on one phone but not on the other.

My guess is why this is happening is that the implementations for Bluetooth and USB support on Android are too buggy or at least diverging in standards.

We do know however that people have standards for music and sound quality, which the manufacturers must abide too or people won't use the product at all. So we are sometimes back to analog modem signals it seems.

May 17, 2015 05:51

Current transformers cannot measure cables with returning current in them also

Posted by admin |

Kind of obvious, but it means you cannot just clip them around a normal power cable, such as are used to power household devices that plug into wall sockets.

 

The primary winding of the CT is the wire carrying the current you want to measure. If you clip your CT around a two or three core cable that has wires carrying the same current but in opposite directions, the magnetic fields created by the two wires are equal and opposite and will cancel each other. Your CT will have no output


Read more: Link - CT sensors - Introduction | OpenEnergyMonitor

May 13, 2015 06:55

ESP8266 - WiFi chip that can be a full microcontroller

Posted by admin |

Including kind of compatible with Arduino. It obviously can do WiFi, but people have now hacked so that you do not need a microcontroller or computer to control it. It can do that by itself, including accepting a lot of Arduino code. ESP8266 Community Forum View topic - Arduino IDE released for ESP8266

This board exposes all the pins, including serial and PWM MOD-WIFI-ESP8266-DEV - Open Source Hardware Board

Lua based firmware nodemcu/nodemcu-firmware

Read more: Link - ESP8266: This $5 Microcontroller with Wi-Fi is now Arduino-Compatible | Make:

May 12, 2015 12:45

Free high quality audio recording on Android

Posted by admin |

Conclusion

Hertz the WAV recorder, Record That Note and PCM Recorder had the best recording quality.

  • Hertz is open source and stays on my phone.
  • Record that note has a VU meter which is quite useful, and you can play and share sound files from the app so it might also stay on my phone.

The contestants

The 6 free apps below for Android claim to record high quality audio in uncompressed form such as WAV or PCM. Apps only briefly tested by me at this point in time, with an LG G2 with its factory headset, and me counting to twenty. Lots of white noise in all recordings and no possibility to adjust recording levels.

  • Hertz, the WAV recorder Hertz is open source software licenced under Apache 2.0. The source code is available, not that many downloads, sounds better than Urecord I think in a crude preliminary test
  • Urecord - Urecord is open-source via the GPL: https://bitbucket.org/thomasebell/urecord, not that many downloads, sounds a bit worse than the other ones I think (or imagine).
  • Sound Recorder + , not that many downloads, only one to go up to 48 KHz (the other ones go to 44.1 KHz) good sound quality, but it skipped samples at 48 KHz so that a whole number is missing in my recording test of counting to twenty. I have not tested with lower sampling frequencies, but I will not use this app.
  • Record That Note Can delete silence (not what I am looking for but can be good for those who want to cut out pauses automatically), not that many downloads, sounds on par with Hertz I think in a crude preliminary test
  • PCM Recorder, 8k downloads, sounds on par with Hertz I think in a crude preliminary test
  • Recordoid Dictaphone Lite Claims high audio quality, does not specify format, 1,6K downloads, wants rights to send SMS, there may be a point to the SMS permission, but I'm avoiding the app

The apps where found through this useful page: 10 Best Apps for Uncompressed Audio (android) | AppCrawlr

 

A better microphone is the obvious upgrade from here. I wonder if the noise comes from the phone or the mic+wire. Furthermore, if the noise is generated by atmospheric noise, would it be possible to mix in a real time noise source in anti phase? Would a low impedance output amplifier help between the mic and phone?

 

May 11, 2015 07:35

Arduino-like cards with built in Bluetooth

Posted by admin |

 

Summary: LightBlue Bean by Punch Through Design seems the most promising.

All cards untested by me at this point in time.

It seems to me that a good use case for the Arduino is as a very low power device that just wakes up briefly, to send some sensor data to a server. Sensor data could be different air quality measurements such as humidity, or certain gases. As long as the bluetooth can connect relatively quickly on wake-up, you ought to be able to make sensoring arduinos that can last for years on a battery. So you could place them in places that are difficult to reach with electrical power.

 

Here is what I have found after a brief search, and as I said I have not tested any of these:

Bluetooth Bee - Standalone (built-in Arduino) - priced at $23.50 when I read the link, needs a shield in order to be programmed, it seems

RF Digital RFduino RFD22102 - Micro Center - at $21, needs an additional USB shield in order to be programmed

BLEduino: Bluetooth 4.0 Made Easy  has a USB port, which might make it less power efficient. Does not seem to be publically available to buy just yet.

Bluz: A cloud-connected, Bluetooth LE development kit by Ben Harris — Kickstarter

At least according to specs, this seems to be what I am looking for: LightBlue Bean – Punch Through Design. It says:

The Bean is the first Arduino-compatible microcontroller board that's wirelessly programmable over Bluetooth Low Energy.

It costs $30 though, so a bit more pricey that the options above. I had to get deep into Google's search results to find LightBlue Bean.

I wonder why the other ones cannot just be programmed on serial pins? Have I missed something?

May 11, 2015 12:35

Controlling a servo with the phone's headset jack

Posted by admin |

Untested by me, but could potentially cut down on the number of components.

 

Controlling a hobby servomotor is relatively easy. It requires a single digital pulse-width modulation (PWM) signal at 50Hz, which means one pulse every 20ms. The width of the “high” pulse sets the absolute rotation angle of the servo.


Read more: Link - Smartphone Servo | Make:

May 09, 2015 01:25

Power consumption of the Raspberry Pi Model B & B+ & an Arduino Uno R3

Posted by admin |



A lightly-loaded model B with keyboard = 1.89 W -> daily 45.36 Wh
A lightly-loaded model B+ with keyboard = 1.21 W -> daily 29.04 Wh
B+ with LAN/USB chip off (no i/o except GPIO) = 0.76 W -> daily 18.24 Wh B+ shut down = 0.26 W -> daily 6.24 Wh
The B+ really offers huge improvements in the power circuitry. Wow!

For an Arduino Uno it seems to be around 230 mW at idle, and putting it to sleep helps a bit mut not that much since the USB is still running it seems, so about a 25% saving only.

It seems that a Raspberry pi B+ can compete with an Arduino Uno in idle in terms of power consumption. You've got to get one of the Arduinos without USB to get to the real low power stuff.
 

Read more: Link - power supply - How much energy does the raspberry pi consume in a day? - Raspberry Pi Stack Exchange

 

How Much Less Power does the Raspberry Pi B+ use than the old model B? » RasPi.TV

Arduino Power Consumption - Gadget Makers' Blog

May 09, 2015 11:50