Python Forum
call method in class using threads?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
call method in class using threads?
#1
Hi python-Volks,

I'm a phyton n00b trying to extend an existing program and I got stuck. Let me
try to explain very briefly the project to illustrate my problem:

1. My program's main loop is basically an event queue waiting for events
2. before this loop, a class "engine" and "time" is called

main.py:
engine = motor()
time = clock()
<set up the rest>
while True
        try:
                event = evt_queue.get()
        except queue.Empty:
                pass
        else:
        <EVENTS BEING PROCESSED>
motor.py:
class motor(object):
        def __init__(self):
                self._receiving_thread
                self._receiving_thread.daemon = True
                self.spawn(self)
        def spawn(self, engine):
                self.engine = engine
                self.process = subprocess.Popen(TELNET CONNECTION)
        def _receiving_thread_target
                while self.is_alive:
                        line = self.process.stdout.readline()
                        if not line:
                                continue
                        self.on_line_received(line)

        def wline(self,string)
                self.process.stdin.write(string)
        def on_line_received(self,buff)
                <process buff>
                fire event in queue
time.py:
class clock(object):
        <this basically runs a threaded real-time clock and is interfaced
         to some hardware which provides a time string using hw_clock_time>

        def hw_clock_time:
                ctime = copy.copy(self.hwclock.time)
                return ctime
This latter method hw_clock_time is called from outside time.py by
other events of the main loop.

My problem is now this:
I want to set the time via hw_clock_time
by sending a command to the telnet connection and
receiving a specific line to return as ctime.

For now I did that by defining a "global" variable (teltime) in the "motor" class and
letting the line-parser (on_line_received) modify this global.
        def hw_clock_time:
                fire(Event.wline("get telnet time")
                if not teltime = None:
                        return teltime
Even though this technically works, this raises timing issues,
as the telnet connection may not respond fast enough to catch the time update and the clock is ticking...
And apart from that: "globals" are considered evil by most python-guys I heard?

As a second attempt, I tried to call wline from inside hw_clock_time but I fail to do so without reinistantiating "engine", which opens a new telnet connection thread each time this is done - which is not what I want.

I guess there is a simple solution to my problem which I simply do not see?


Thanks in advance and best regards

Masterofamn
Reply


Messages In This Thread
call method in class using threads? - by masterofamn - Jan-24-2017, 09:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  class definition and problem with a method HerrAyas 2 225 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() and order of running method in class inheritance akbarza 7 721 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  method call help sollarriiii 6 1,142 Feb-21-2023, 03:19 AM
Last Post: noisefloor
  Using one child class method in another child class garynewport 5 1,564 Jan-11-2023, 06:07 PM
Last Post: garynewport
  How to call a class in other class? 3lnyn0 3 923 Oct-24-2022, 09:18 AM
Last Post: GenTorossi
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,306 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,454 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,756 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,698 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  anonymous method in a class Skaperen 8 3,571 May-23-2021, 11:17 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020