472,128 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,128 software developers and data experts.

RE: Continually check object status

updated creature running in its own thread will get you started. try it foryourself, change sleep times per your need.

import os, sys, threading, time
class Creature:
def __init__(self, status):
self.status = status
self.state = 'run'

def start(self):
self.athread = threading.Thread(target=self.print_status)

def change_status(self, new_status):
self.status = new_status

def print_status(self):
while self.state == 'run':
print self.status

def stop(self):
self.state = 'stop'

#main loop
c = Creature('happy')
time.sleep(3) #wait some time
time.sleep(3) #wait some more time
concept would be similar with GUI as well

-----Original Message-----
From: py************************************************ **@python.org
[mailto:py***************************************** *********@python.org]
On Behalf Of fu*********@gmail.com
Sent: Saturday, August 02, 2008 1:54 PM
To: py*********@python.org
Subject: Re: Continually check object status
On Aug 2, 1:05*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
futileis...@gmail.com schrieb:
Beginner, so please bare with me. *I'm not sure what to call what it
is I'm looking for.
If I have an object class, let's call it "Creature":
class Creature:
* * def __init__(self, status):
* * * * self.status = "happy"
* * def change_status(self, new_status):
* * * * self.status = new_status
* * def print_status(self):
* * * * print self.status
I would like to be able to print out the Creature's status every 20
seconds. *Let's say I use a script like this:
import time
while True:
* * time.sleep(20)
* * Creature.print_status()
But, while cycling through printing the status, I would like to be
able to update Creature.status to something new.
I might be approaching this from the wrong direction entirely. *Thanks
for your input.

The "simple", yet possibly dangerous answer is: you need
multi-threading. Multi-threading is a technique that allows several
(quasi)-parallel paths of execution whilst sharing memory and objects
inside that memory. The module in python to achieve this is called

However, concurrent programming is a very advanced topic, ridded with
pitfalls for even experienced developers.

There are other ways to solve the problem, commonly known as event-loops
and timers. These are usually part of frameworks for e.g GUI-creation an
such, but you can also roll your own if you like.

So, the better answer might be a question: what do you ultimately want
to achieve? Given the name of your class, Creature, I assume you are
writing on some game or such. Depending on how you plan to do that, you
might have a framwork providing you with the needed tools/library calls
or whatever.

I was afraid that someone was going to mention threading. I have read
about it before but not been able to do much with it.

My ultimate goal is to create some sort of tamagotchi style virtual
pet to interact with. Over time it gets hungry or bored, but the
process can be fixed by a user "feeding" or "playing with" it. I
wanted to take this opportunity to teach myself some PyGTK coding as
well, but I thought that maybe I could build the creature object and
looping in such a way that it would be possible to add a GUI to it
The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure. If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof. Thank you.
Aug 2 '08 #1
0 1210

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by gallaczmit | last post: by
1 post views Thread by Vanessa | last post: by
4 posts views Thread by tshad | last post: by
reply views Thread by kevin bailey | last post: by
5 posts views Thread by futileissue | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.