06Jan Killing a thread in Python

I have been searching for quite a while for information on how to kill a thread in Python and I finally found a great solution. I was just about to implement my own way of doing it but it is not as clean as this way (although killing threads is not clean in the first place).

My thought was to use threading.settrace() and set my own trace function for every thread that is started. That way every thread will have to pass through the trace function in oder to continue executing and it doesn’t matter where in the code it is at or going. In my trace function I was going to add an Event that it would wait on if I want to pause execution of the thread, or check something to make it through an execption to exit out of the thread.

I was in the process of searching for a way to find out if a thread died because of an Exception when I ran across a post here: http://www.dlevel.com/blogs/alex/20

What they have done is added a terminate() method to the Thread class in the threading library. This will throw a SystemExit exception and quitely terminate the thread. Just take the code and paste it into a new module and use that Thread class as opposed to the threading.Thread class.

I agree with what he says about using this. Obviously it is not very safe to do this but in some cases you really need a way to terminate a thread and if you are careful it can work out ok.

Tags:

31Dec Interesting Python Static Attributes

So I was playing around with some code in Python today and was  curious about static members of a class (I guess that is what you would call it). I wanted to know if I set an attribute at the class level and change it, would all the instances see that change or if each instance is separate.

Here is some code I played with:


>>> class T:
... build_location = None
... def get_location(self):
...    return self.build_location
...
>>> t = T()
>>> print t.get_location()
None
>>> T.build_location = "hello"
>>> print t.get_location()
hello
>>> t.build_location = "there"
>>> print t.get_location()
there
>>> print T.build_location
hello
>>> T.build_location = "hello"
>>> print t.get_location()
there
>>>

So, what I did was set an attribute on the class level that everyone can see without an instance of the class. When I create an instance it can see that attribute just like I can from just doing T.build_location. When I change the static variable then the instance sees that change as well (which is what I was hoping for).

Now, the interesting part is that if I use an instance of the class to change the variable, that variable becomes local to that instance as you can see above when I printed the class’s T.build_location. Now I tried to set the class level attribute back to “hello” and that works but now it does not change what the instance sees!

This is not any earth shattering news but I thought it was interesting when I saw it.

Tags:

15Dec Find the number of files in a directory

So I needed to find the number of files in a directory recursively. I found a great way to figure that out on Linux that has helped me a couple times now. I definitely need to remember it…anyway here is is:

find /my/directory -type f |wc -l

If you need to find the number of directories recursively then do:

find /my/directory -type d | wc -l

Find will just print out all the files it finds. It you tell find a type to find it will only print files of that type (f for file, d for directory). wc will just count the number of lines in the output

Tags:

03Dec Twitter Me This

So, I just started using twitter and it is pretty cool. One of the best things I think on facebook is seeing people’s status change…it can show what they are going through or doing right now. Twitter is a little microblogging tool that you basically just state your status like you would on facebook and thats it. You can get updates in text message form sent to your form whenever someone adds something to twitter.

I love it. Sometimes it takes too long to write a whole blog entry but it is easy to send little status updates in my free seconds…the question is, how do I get family members and friends to watch that for my status updates without having them be forced into getting an account and always logging in to see the updates…

Maybe there is a Wordpress plugin I can use to show it on my personal blog or some sort of RSS thing to check out. Either way I think I like twitter…although they need to add more ways to send and receive updates, like through email, IM, or RSS or something, because I don’t get unlimited text messages so it can definitely ruin the experience.

Tags: ,

03Dec Numpad not working in Ubuntu

For the last several months on my Ubuntu machine at work the numpad has not been working. It has been so frustrating. I would turn the numlock on and off and nothing would seem to get it to work.

I thought it was something wrong with the keyboard or possibly after a kernel update one of the drivers changed and killed it for some reason. I was on Hardy and haven’t taken the time to update to Intrepid yet. I just figured after I update to Intrepid it would fix the problem. Turns out it probably would have but I fixed it without having to upgrade yet.

This problem actually has to do with Gnome’s Accessibility settings! I found out that if I type 2,4,6, or 8 on the key pad it moved the mouse around by tiny amounts, almost unnoticeable.

To fix the problem I just went to System -> Preferences -> Assistive Technologies, clicked on Mouse Accessibility, selected the mouse keys tab and unchecked the “Allow to control the pointer with keyboard” option. That fixed it!

Tags: ,

08Nov SSH Filesystem

Holy cow, this about blew my mind. I was working on a simple little website that had no FTP access so I was just planning on copying all the files down to check out the site and see how things were working. Obviously it is not fun to do that. Well I did have SSH access to the server and so I was trying to see what images were which, well, I didn’t really want to scp all the files I wanted to preview one-by-one. Oh, and I didn’t have root access and wasn’t sure if it had NFS available or not. That’s when I found out about sshfs!

With sshfs I can mount all the files locally over an SSH connection! To do this I:

sudo apt-get install sshfs

Edit /etc/modules and add a line that says ‘fuse’ if it is not there already

Add myself to the fuse user group:

sudo adduser username fuse

Then just mount the filesystem:

sshfs username@host:/path/to/remote/files /media/mount-point

Then to unmount do:

fusermount -u /media/mount-point

That’s it. It was so nice to work on all the files using my local tools and apps. Obviously this is not the best idea for changing a production server but it just provides a little nicer look at the remote files.

Tags: ,

06Nov Gnome Default Programs

I had installed Adobe Reader to replace the default eVince document viewer. The problem was that PDFs I opened in Nautilus still opened with eVince. I wanted to know how to fix it so it would always open with Adobe Reader.

In order to do that you need to navigate to the file you want to open with a certain program in Nautilus. Right click on the file and click on Properties. Open the “Open With” tab. Select the program you want to open the file.

Thats it! So easy, and much better than the Windows way of doing it, you just have to know where to go.

Tags: ,