473,473 Members | 1,415 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

changing params in while loop

hi,

i'm a newbie, but please bear with me for a second.
i have this function inside a while-loop, which i'd like to loop
forever, but i'm not sure about how to change the parameters of my
function once it is running.
what is the best way to do that? do i have to use threading or is there
some simpler way?
here's some code to show the structure of the problem:

from time import *

b = 44

def printStuff(a):
print a
sleep(1)
print b
sleep(1)

while 1:
printStuff("bibi")

now how can i make printStuff() print "fifi" instead of "bibi" or 88
instead of 44 once it is running?

thank y'all for your help
do the snake!

robin

Feb 28 '06 #1
1 1482
robin wrote:
i have this function inside a while-loop, which i'd like to loop
forever, but i'm not sure about how to change the parameters of my
function once it is running.
what is the best way to do that? do i have to use threading or is there
some simpler way?

Why not just do this inside the function? What exactly are you trying
to accomplish here? Threading could work here, but like regexes,
threads are not only tricky to get right but also tricky to know when
to use in the first place.

That being said, here's some example threading code to get you started
(PrinterThread.run is your function; the ThreadSafeStorage instance
holds your parameters):

import threading
import time

class ThreadSafeStorage(object):
def __init__(self):
object.__setattr__(self, '_lock', threading.RLock())
def acquirelock(self):
object.__getattribute__(self, '_lock').acquire()
def releaselock(self):
object.__getattribute__(self, '_lock').release()
def __getattribute__(self, attr):
if attr in ('acquirelock', 'releaselock'):
return object.__getattribute__(self, attr)
self.acquirelock()
value = object.__getattribute__(self, attr)
self.releaselock()
return value
def __setattr__(self, attr, value):
self.acquirelock()
object.__setattr__(self, attr, value)
self.releaselock()

class PrinterThread(threading.Thread):
"""Prints the data in shared storage once per second."""
storage = None
def run(self):
while not self.storage.killprinter:
self.storage.acquirelock()
print 'message:', self.storage.message
print 'ticks:', self.storage.ticks
self.storage.ticks += 1
self.storage.releaselock()
time.sleep(1)

data = ThreadSafeStorage()
data.killprinter = False
data.message = 'hello world'
data.ticks = 0
thread = PrinterThread()
thread.storage = data
thread.start()
# do some stuff in the main thread
time.sleep(3)
data.acquirelock()
data.message = 'modified ticks'
data.ticks = 100
data.releaselock()
time.sleep(3)
data.message = 'goodbye world'
time.sleep(1)
# notify printer thread that it needs to die
data.killprinter = True
thread.join()

# output:
"""
message: hello world
ticks: 0
message: hello world
ticks: 1
message: hello world
ticks: 2
message: modified ticks
ticks: 100
message: modified ticks
ticks: 101
message: modified ticks
ticks: 102
message: goodbye world
ticks: 103
"""

--Ben

Feb 28 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: bill mahoney | last post by:
I have access 2k and I have 2 tables. One is a 7X6 table representing a month with zeroes padding the fields where no day exist. For example February 2003 0 0 0 0 0 0 1 2 3 4 5 6 7 ...
3
by: Jeff | last post by:
Hi I have a report with a graph on it and want to change the minimum and maximum values for the value axis when it is previewed. This can't be done by adding code in the Open event as once the...
0
by: Alan Chen | last post by:
Hi, I am writing my first web service program, I am puzzled that I can't use params while I have no problem in a console app. public string Request( params object elements ) { //code...
10
by: Altman | last post by:
I have only done a little programming in C++ so I am still learning but I am having a problem with a variable that is changing on me. I have tried this 2 ways with the same result. I have a...
4
by: chuckdfoster | last post by:
Can I change the page title from my ASP.NET code? I know you can from classic ASP. Thanks in advance. -- Chuck Foster Programmer Analyst Eclipsys Corporation - St. Vincent Health System
0
by: Mathan Babu | last post by:
Hello, Once the AJAX request is fired the readystate property remain as 1. It never gets changed and the callback method is not firing as a result the ..aspx page is not being rendered. The...
2
by: boyleyc | last post by:
Hi all the following code works perfectly well. Basically it populates a series of check boxes on my form, depending on whether dlookup finds an associated record. The problem i have is that...
9
by: Andreas Schmitt | last post by:
I am somewhat new to C# and I ran into a problem in a small program I am writing for teaching myself. I am handling a list ob objects and I want to delete some of them inside a loop like in: ...
1
by: David | last post by:
"David" <davidd31415@yoowhoo.comwrote in message news:3dmdnQe_FOWLdWHanZ2dnUVZ_veinZ2d@comcast.com... No luck yet. I created a service from scratch but keep getting the "service on local...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.