I have a text file which acts as a source for program A and sink for 
program B. Program B responsible for updating the file does so in regular 
intervals of time and always overwrite the file. Program A that is reading 
the file is constantly reading it and takes in the updated values whenever 
that happens. It works fine for sometime. But eventually, Program A 
crashes. The error shown is:

File "/home/../baba_rect.py", line 61, in on_draw
     if (int(a[core_id]) <= 100):
 ValueError: invalid literal for int() with base 10: '' 

Here is the code for Program A:

    import sys, time, math, os, random
    from pyglet.gl import *

    red = [255,0,0]
    green = [0,255,0]
    blue = [0,0,255]
    black = [0,0,0]

    dim_x = 2;
    dim_y = 2;

    window = pyglet.window.Window()

    label = pyglet.text.Label('Simulation', 
                              font_name='Times New Roman', 
                              font_size=16,
                              color=(204,204,0,255),      #red font (255,0,0) 
opacity=255
                              x=window.width, y=window.height,
                              anchor_x='right', anchor_y='top') 

    class FilledSquare:
        def __init__(self, width, height, xpos, ypos):
            self.xpos = xpos
            self.ypos = ypos
            self.angle = 0
            self.size = 1
            x = width/2.0
            y = height/2.0
            self.vlist = pyglet.graphics.vertex_list(4, ('v2f', [-x,-y, x,-y, 
-x,y, x,y]), ('t2f', [0,0, 1,0, 0,1, 1,1]))  
        def draw(self,w,h,x,y):
            self.width=w
            self.height=h
            self.xpos=x
            self.ypos=y
            glPushMatrix()
            glTranslatef(self.xpos, self.ypos, 0)
            self.vlist.draw(GL_TRIANGLE_STRIP)        
            glPopMatrix()


    @window.event
    def on_draw():
        window.clear()
        glClearColor(0, 0.3, 0.5, 0)
        glClear(GL_COLOR_BUFFER_BIT)
        label.draw()

        for i in range(0,dim_x):
            for j in range(0,dim_y):
                print i,",",j
                #read file
                f = open('r.txt', 'r')
                a = f.readline()
                a = a.rstrip('\n')
                a = a.split(" ")
                f.close()
                #core_id
                core_id = j*dim_x + i;
                if (int(a[core_id]) <= 100):
                    color = red
                elif (int(a[core_id]) <= 200):
                    color = green
                elif (int(a[core_id]) <= 300):
                    color = blue
                else:
                    color = black

                glColor3f(color[0], color[1],color[2])
                squares[i+j].draw(60,60,i*50+200,j*50+200)     

    squares = [FilledSquare(30, 30, 0, 0), FilledSquare(30, 30, 0, 0), 
FilledSquare(30, 30, 0, 0), FilledSquare(30, 30, 0, 0)]   # 2x2

    pyglet.app.run() 

This is how the text file *r.txt* may look like at any instant in time:

10 20 120 235

I believe the reason for the crash is because at that point, it so happens 
that both Pgm A and Pgm B are accessing the file simultaneously and Pgm A 
reads a *" "* instead of the number which can't be converted to int(). If 
my conclusion is indeed true, then how can I overcome this?

Or: Is it possible to make pyglet.app.run() sensitive to any change to 
*r.txt*? i.e. Pgm A runs only when there is a change in the text file, thus 
avoiding the conflict.

-- 
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to