[Tutor] Thread question

Steven D'Aprano steve at pearwood.info
Thu Oct 18 01:37:38 CEST 2012


Mike,

A brief comment before I start:

your post would be a lot more readable and understandable if you were to
break it up into smaller paragraphs, each of which explains one point.
Otherwise it is a great wall of text which is fairly intimidating.


On 18/10/12 06:51, Mike wrote:

> My program is command line based and is driven by the user making a
> selection from the menu. Basically option 1 perform a HTTP GET request
> to xyz.com/?get=test.  The user supply's a list  of target domains and
> the query (?get=test) is static.

No it isn't. You go on to explain below that there are at least two
different queries:

?get=test
?get=check

and presumably no query at all (when param='').

Since the query is not static, but part of the request, it should be part
of the data pushed onto the queue. Instead of queuing just the domains,
queue a tuple (domain, query) or even the full URL.


> This works great if the user selects option 1, but I am
> running into problems when a user selects multiple options.  Say a
> user selection 1 then option (2) with the targets being the same, but
> the query string is now ?get=check. The problem I am facing is when
> the threads run with option 1 and hit the initialization they are set
> to ?get=test i.e. self.param=param. The param is passed in to the init
> of the thread class.  When I initialize the class from option 1 I pass
> in the param ?get=test and when I initialize the class with option 2 I
> pass in the param ?get=check.  The default initialization is
> self.param="".

Why is the query an attribute of the *thread*? The query is an attribute
of the request, not the thread.

Change your design, and the problem goes away.


> Depending on which option they select first the thread
> will be one of the following: blank, or one of the two options.  I
> don't know how to update the thread variable when it's running i.e.def
> run(self).

mythread.param = new_value

but please don't do this. The design is wrong, you have wrongly assumed
that the param is a part of the thread when it is actually a part of the
request.


> I realize this sounds all confusing, but  I am basically
> wondering how one would update a thread variable when it is running?
> Also, is it best to spawn threads each time the user make a selection
> or once when the program starts?

I would say, neither. It is best to spawn threads when they click the
"Make it go" button. Decide how many threads you need, spawn them, and
let them run.


-- 
Steven


More information about the Tutor mailing list