2

I am writing a code to use scale to update a global value VOL. The initial value of VOL can be passed to function "check_keys", however, when the scale changes its value, VOL remains unchanged in the thread using function "check_keys". Where was I doing wrong?

#!/usr/bin/python
from Xlib.display import Display
from multiprocessing import Process
import threading
import os
import time
import gtk

VOL=1
RUNNING=0

class StatusIcon:
    def __init__(self):
        global RUNNING
        global VOL

        self.statusicon = gtk.StatusIcon()
        self.statusicon.set_from_stock(gtk.STOCK_HOME)
        #self.statusicon.connect("popup-menu", self.right_click_event)

        self.win_build()

        RUNNING=1
        pp = Process(target=self.check_keys)
        pp.start()      

    def quit_program(self, widget):
        global RUNNING
        RUNNING=0
        gtk.main_quit

    def scale_moved(self, widget):
        global VOL
        #print self
        #print VOL
        VOL = self.scale.get_value()
        #print VOL

    def check_keys(self):
        global VOL
        global RUNNING
        disp = Display()
        hold = 0
        samp_inv=0.02
        while RUNNING:
            print "RUNNING"
            print RUNNING
            print "VOL"
            print VOL
            time.sleep(0.2)
    def win_build(self):
        global VOL

        self.window = gtk.Window()
        self.window.set_default_size(400, -1)

        adjustment = gtk.Adjustment(VOL, 0, 150, 5, 10, 0)
        self.scale = gtk.HScale(adjustment)
        self.scale.set_digits(0)
        self.scale.set_update_policy(gtk.UPDATE_DELAYED)

        #VOL=1.
        self.visible=1
        self.window.connect("destroy", lambda w: w.hide_all())
        self.scale.connect("value-changed", self.scale_moved)

        self.window.add(self.scale)
        self.window.set_title("Typewriter Sound Maker")

        self.window.set_position(gtk.WIN_POS_CENTER_ALWAYS)

        self.window.show_all()

StatusIcon()
gtk.main()
2
  • Could you add the missing function: self.right_click_event(), so one can test the code? Commented Dec 27, 2013 at 3:22
  • Thanks isset, right_click_event() is irrelevant, I have removed it so the code can run. Commented Dec 27, 2013 at 12:06

1 Answer 1

7

I think the main problem here is: You are using multiprocessing and not threading.

When you use multiprocessing, python will actually fork a new Process, when that happens it can't access the memory of the other process anymore.

You have 2 Options here:

The down side is that it's not really parallel, because Python is doing the context switch, so one part of your app will be blocked. Anyway, I don't think that concerns you here, because you aren't looking for performance benefits.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.