Having problems accepting parameters to a function

rh0dium steven.klass at gmail.com
Tue May 1 17:42:18 EDT 2007


Thanks to all who helped!!

Let me expand a bit more - I am working on a threading class and I
want to be able to push on the Queue a list of args.  If you run the
following program - I am failing to understand how to push items onto
the queue in a manner so that func2 recognizes them as kwargs not as
args.  Can anyone help me with this.

test1 works
test2 fails again I can't figure out how to push onto the queue a
dictionary and get it back off.

 I know this is a lot longer than I tend to post but I really want to
solve this bugger.


Any help is greatly appreciated.

------------<snip>----------------

#!/usr/bin/env python
# encoding: utf-8
"""
myThreading.py
"""

import sys
import os
import traceback
import logging
import threading
import Queue

LOGLEVEL=logging.DEBUG

# Basic logger
logging.basicConfig(level=LOGLEVEL, format="%(asctime)s %(name)s %
(levelname)-8s %(message)s",
                    datefmt='%d %b %Y %H:%M:%S', stream=sys.stderr)

# Identifies (hopefully) the current module name
try:
    module= os.path.basename(traceback.extract_stack(limit=2)[1]
[0]).split(".py")[0]+"."
except:
    module = os.path.basename(traceback.extract_stack(limit=2)[0]
[0]).split(".py")[0]+"."

class myThread(threading.Thread):

    def __init__(self, *args, **kwargs):
        """description"""

        self.id = kwargs.get('id', 0)
        self.requestQ = kwargs.get('requestQ', None)
        self.responseQ = kwargs.get('responseQ', None)
        self.function = kwargs.get('function', None)

        # Setup Logging
        self.log = logging.getLogger(module+self.__class__.__name__
+"."+str(self.id))
        self.loglevel = kwargs.get('loglevel', logging.WARN)
        self.setLoglevel(self.loglevel)

        self.log.debug("Starting Thread %d" % self.id)
        threading.Thread.__init__(self, name=module
+self.__class__.__name__+"."+str(self.id))

    def setLoglevel(self,loglevel):
        """setLog log level"""
        if loglevel is not False:
            self.log.setLevel(loglevel)
            self.log.debug("Setting Logging level to %s" % loglevel)
        else:
            self.log.setLevel(logging.WARN)

        if self.loglevel == logging.DEBUG: self.debug = True
        else: self.debug=False

    def run(self):
        while 1:
            input = self.requestQ.get()
            if input is None:
                self.log.debug("Ending the thread - Recieved None")
                if self.responseQ is not None:
                    self.responseQ.put(None)
                break
            self.log.info("Applying %s to function %s" %
(str(input),str(self.function.__name__)))
            result = self.function(input)
            if self.responseQ is not None:
                self.log.debug("Response recieved = %s" % result)
                self.responseQ.put(result)

def func(input):
    import time
    time.sleep(input)
    return 2*input

def test1(loglevel=False):

    log = logging.getLogger(module+sys._getframe().f_code.co_name )
    if loglevel is not False: log.setLevel(loglevel)
    else:log.setLevel(logging.WARN)

    maxThreads=5

    # Set up two queues one for sending request data (req) one for
getting response to the request (res)
    reqQ = Queue.Queue()
    resQ = Queue.Queue()

    # Push some data onto the reqestQ end it with None
    import random
    for x in range(200): reqQ.put(random.random())
    for n in range(maxThreads): reqQ.put(None)

    # Start Up some threads to do some work
    for n in range(maxThreads):
        t = myThread(id=n,loglevel=logging.INFO, function=func,
requestQ=reqQ, responseQ=resQ).start()

    # Collect the results
    results = 0
    while 1:
        try:
            data = resQ.get()
            if data is None:
                break
            else:
                results += data
        except:
            break
    print results

def func2( input, loglevel=False ):

    import time

    #print "args", args
    #print "kwargs", kwargs

    log = logging.getLogger(module+sys._getframe().f_code.co_name )
    #loglevel = kwargs.get('loglevel', logging.WARN)
    log.setLevel(loglevel)

#    input = kwargs.get('input', 0.0)
    log.debug("Using input %s" % str(input))
    time.sleep(input)
    return 3*input

def test2(loglevel=False):

    log = logging.getLogger(module+sys._getframe().f_code.co_name )
    if loglevel is not False: log.setLevel(loglevel)
    else:log.setLevel(logging.WARN)

    maxThreads=5

    # Set up two queues one for sending request data (req) one for
getting response to the request (res)
    reqQ = Queue.Queue()
    resQ = Queue.Queue()

    # Push some data onto the reqestQ end it with None
    import random
    for x in range(5): reqQ.put({'input':random.random(),
'loglevel':loglevel})
    for n in range(maxThreads): reqQ.put(None)

    # Start Up some threads to do some work
    for n in range(maxThreads):
        t = myThread(id=n,loglevel=logging.INFO, function=func2,
requestQ=reqQ, responseQ=resQ).start()

    # Collect the results
    results = 0
    while 1:
        try:
            data = resQ.get()
            if data is None:
                break
            else:
                results += data
        except:
            break
    print results

def main(loglevel=False):
    """ """
    # Setup Logging
    log = logging.getLogger(module+sys._getframe().f_code.co_name )
    if loglevel is not False: log.setLevel(loglevel)
    else:log.setLevel(logging.WARN)

    # test1(loglevel)
    test2(loglevel)


if __name__ == '__main__':
    sys.exit(main(loglevel=LOGLEVEL))





More information about the Python-list mailing list