BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Multitasking, how do i solve this one?

Tue Sep 04, 2012 12:46 pm

@Aydan: We are not talking about a regular file here but a device driver. If it can not cope with more than one line I doubt it implements seek behaviour. Flushing might be worth a try.

Code: Select all

while not self.asleep():
    sheep += 1

Aydan
Posts: 700
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: Multitasking, how do i solve this one?

Tue Sep 04, 2012 12:50 pm

BlackJack wrote:@Aydan: We are not talking about a regular file here but a device driver. If it can not cope with more than one line I doubt it implements seek behaviour. Flushing might be worth a try.
I know, but I had a similar problem implementing a class for GPIO usage, where I had to seek(0) before a new value would be accepted. This is probably implementation dependent. I have no idea how this "file" mechanism works though. Is it using a pipe? If so a seek would indeed be useless.

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Multitasking, how do i solve this one?

Tue Sep 04, 2012 1:05 pm

@Aydan: Simplified: A driver registers functions that will get called at various events like a program calling open on the file or writing/reading data to/from it.

Code: Select all

while not self.asleep():
    sheep += 1

rgh
Posts: 212
Joined: Fri Nov 25, 2011 3:53 pm

Re: Multitasking, how do i solve this one?

Tue Sep 04, 2012 6:44 pm

Python is trying to be efficient and combining multiple write() calls in user space in to a single write to the driver. That makes sense normally, but not for my driver :) You can get round that doign explicit flush calls like so:

Code: Select all

s1=0
s2=0
sb=open("/dev/servoblaster","w")
sb.write("0="+str(s1)+"\n")
sb.flush()
sb.write("1="+str(s2)+"\n")
sb.flush()
sb.close()
Then you can see in strace output that it does an open, two writes, and a close, all successful:

Code: Select all

[email protected]:~/ServoBlaster$ strace ./tst.py
...
open("/dev/servoblaster", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
...
write(3, "0=0\n", 4)                    = 4
write(3, "1=0\n", 4)                    = 4
close(3)                                = 0
...

bulldog5046
Posts: 36
Joined: Thu Jun 21, 2012 1:50 pm

Re: Multitasking, how do i solve this one?

Tue Sep 04, 2012 6:58 pm

Cool,

Just interested, if you did it my old method when i ran 2x open.write and didn't close, does any close show up on strace?

rgh
Posts: 212
Joined: Fri Nov 25, 2011 3:53 pm

Re: Multitasking, how do i solve this one?

Tue Sep 04, 2012 7:09 pm

This python code results in kernel calls of open,write,close,open,write,close:

Code: Select all

open('/dev/servoblaster','w').write('0='+str(s1)+'\n')
open('/dev/servoblaster','w').write('1='+str(s2)+'\n')

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Multitasking, how do i solve this one?

Tue Sep 04, 2012 7:13 pm

But the close is not guaranteed by the language specification to happen directly after the `write()`.

Code: Select all

while not self.asleep():
    sheep += 1

bulldog5046
Posts: 36
Joined: Thu Jun 21, 2012 1:50 pm

Re: Multitasking, how do i solve this one?

Tue Sep 04, 2012 8:23 pm

I do see your point.

Maybe I'm just being a bit savage in trying to make my code a lite as possible.

I'll try and keep it by the book but in these learning stages we tend to question 'why' alot :D

Ry

Return to “Python”