473,385 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Question about thread

Refering to the following codes I found, there is nothing displayed in the
console, may I ask why?

def thrd(param): # the thread worker function
print "Received",param

import thread
for i in range(5): # start five threads passing i to each one
thread.start_new_thread(thrd,(i,))

Thanks in advance
Jul 18 '05 #1
15 1564
To be more precise, what I want to do is to have a threaded program to handle
some jobs concurrently (while those jobs are fetching information from the
Internet, so threading could speed up the process if the network is slow)

start
| (blahblahblah...)
v
+-----+-----+-----+-----+
| | | | |
--+-- --+-- --+-- --+-- --+--
| | | | | | | | | |
| A | | B | | C | | D | | E |
| | | | | | | | | |
--+-- --+-- --+-- --+-- --+--
| | | | |
+-----+-----+-----+-----+
| (blahblahblah...)
v
finish!

While process A-E will access the shared variables within the same program, and
each of those processes will have to parse a document on its own.
Valkyrie wrote:
Refering to the following codes I found, there is nothing displayed in the
console, may I ask why?

def thrd(param): # the thread worker function
print "Received",param

import thread
for i in range(5): # start five threads passing i to each one
thread.start_new_thread(thrd,(i,))

Thanks in advance

Jul 18 '05 #2
"Valkyrie" <va******@cuhk.edu.hk> wrote in message
news:1100875373.911763@eng-ser6...
Refering to the following codes I found, there is nothing displayed in the
console, may I ask why?

def thrd(param): # the thread worker function
print "Received",param

import thread
for i in range(5): # start five threads passing i to each one
thread.start_new_thread(thrd,(i,))


You may ask, but when I tried your code, here is what happened:

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
def thrd(param): # the thread worker function .... print "Received",param
.... import thread
for i in range(5): # start five threads passing i to each one .... thread.start_new_thread(thrd,(i,))
....
1960
836
232
2864
3692 Received 0

Received 1
Received 2
Received 3
Received 4

Note that the numbers 1960, etc., appear to be the return values of the
thread.start_new_thread() function.
--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
Jul 18 '05 #3
When I do it line by line in python's console, I have similar result to youl.
But when I try to run it in a file, say:

python demo.py

It just returns me nothing. I have no idea on this right now...

Russell Blau wrote:
"Valkyrie" <va******@cuhk.edu.hk> wrote in message
news:1100875373.911763@eng-ser6...
Refering to the following codes I found, there is nothing displayed in the
console, may I ask why?

def thrd(param): # the thread worker function
print "Received",param

import thread
for i in range(5): # start five threads passing i to each one
thread.start_new_thread(thrd,(i,))

You may ask, but when I tried your code, here is what happened:

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
def thrd(param): # the thread worker function
... print "Received",param
...
import thread
for i in range(5): # start five threads passing i to each one
... thread.start_new_thread(thrd,(i,))
...
1960
836
232
2864
3692
Received 0


Received 1
Received 2
Received 3
Received 4

Note that the numbers 1960, etc., appear to be the return values of the
thread.start_new_thread() function.

Jul 18 '05 #4
Valkyrie wrote:
Refering to the following codes I found, there is nothing displayed in the
console, may I ask why?

def thrd(param): # the thread worker function
print "Received",param

import thread
for i in range(5): # start five threads passing i to each one
thread.start_new_thread(thrd,(i,))

Thanks in advance


The problem with your script is that it will run too fast before it quits and
kills all the threads.

if you add

a = 0
for i in range(100000):
a = a + 1

to the end you will see the output.
Jul 18 '05 #5
On Fri, 2004-11-19 at 23:45, Valkyrie wrote:
When I do it line by line in python's console, I have similar result to youl.
But when I try to run it in a file, say:

python demo.py

It just returns me nothing. I have no idea on this right now...


You need to cause the main thread to wait on the child threads until
they've all finished, otherwise - as Peter Hickman just mentioned - your
script will terminate and all threads will end. Ten seconds on Google
found this:

http://www.faqts.com/knowledge_base/view.phtml/aid/1364

--
Craig Ringer

Jul 18 '05 #6
Thank you for your advice. I have quite seriously searched for it in fact, but
just without the luck on finding such article...

Craig Ringer wrote:
On Fri, 2004-11-19 at 23:45, Valkyrie wrote:
When I do it line by line in python's console, I have similar result to youl.
But when I try to run it in a file, say:

python demo.py

It just returns me nothing. I have no idea on this right now...

You need to cause the main thread to wait on the child threads until
they've all finished, otherwise - as Peter Hickman just mentioned - your
script will terminate and all threads will end. Ten seconds on Google
found this:

http://www.faqts.com/knowledge_base/view.phtml/aid/1364

--
Craig Ringer

Jul 18 '05 #7
Valkyrie wrote:
Thank you for your advice. I have quite seriously searched for it in fact, but
just without the luck on finding such article...

Can you suggest where this information might have shown up
that you would have found it? If so, you may want to submit
a documentation bug or patch request. That is, repay the
group by making it more likely that the next person with your
problem finds their answer on their own.

--Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #8
Craig Ringer wrote:
When I do it line by line in python's console, I have similar result to youl.
But when I try to run it in a file, say:

python demo.py

It just returns me nothing. I have no idea on this right now...


You need to cause the main thread to wait on the child threads until
they've all finished, otherwise - as Peter Hickman just mentioned - your
script will terminate and all threads will end. Ten seconds on Google
found this:

http://www.faqts.com/knowledge_base/view.phtml/aid/1364


Are you sure? I keep observing that child threads continue its execution
until completion even if I do nothing in main thread. Consider this example:

import random, time
from threading import Thread

class Worker(Thread):

def __init__(self, name):
Thread.__init__(self)
self.name = name
print 'thread %s created' % self.name

def run(self):
amt = random.randint(1, 4)
print 'thread %s waiting %d seconds' % (self.name, amt)
time.sleep(amt)
print 'thread %s finished' % self.name

def __del__(self):
print 'thread %s is being destroyed' % self.name

if __name__ == '__main__':
for i in range(10):
t = Worker('%.04d' % (i + 1, ))
t.start()
print 'finished creating threads'

Even after printing "finished creating threads" all spawned threads
continue its execution up to final "thread nnnn is being destroyed", so
I think there's no need to take any special action (unless it's Python
on iSeries, buy this is another story) to wait for completion.

--
Jarek Zgoda
http://jpa.berlios.de/ | http://www.zgodowie.org/
Jul 18 '05 #9
>
Even after printing "finished creating threads" all spawned threads
continue its execution up to final "thread nnnn is being destroyed", so
I think there's no need to take any special action (unless it's Python
on iSeries, buy this is another story) to wait for completion.


The OP used the module "thread", while you used "threading" and didn't set
setDaemon(False). Then python waits until all threads are finished.

From the thread module docs:
When the main thread exits, it is system defined whether the other threads
survive. On SGI IRIX using the native thread implementation, they survive.
On most other systems, they are killed without executing try ... finally
clauses or executing object destructors.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #10
On Sat, 2004-11-20 at 04:56, Jarek Zgoda wrote:
Are you sure? I keep observing that child threads continue its execution
until completion even if I do nothing in main thread. Consider this example:


Well, I'm very far from an expert, but I've also experienced the
poster's reported behaviour even with quite simple programs. I'm
surprised to hear that the initially described behaviour isn't always
the case, and would be interested in knowing more about when it is, and
is not, generally encountered.

--
Craig Ringer

Jul 18 '05 #11
Diez B. Roggisch wrote:
Even after printing "finished creating threads" all spawned threads
continue its execution up to final "thread nnnn is being destroyed", so
I think there's no need to take any special action (unless it's Python
on iSeries, buy this is another story) to wait for completion.
The OP used the module "thread", while you used "threading" and didn't set
setDaemon(False). Then python waits until all threads are finished.


Well... I was bit too fast in replying. ;)
From the thread module docs:

When the main thread exits, it is system defined whether the other threads
survive. On SGI IRIX using the native thread implementation, they survive.
On most other systems, they are killed without executing try ... finally
clauses or executing object destructors.


So it looks that using threading should be preferred over using thread,
as presented example works identically on Windows and on linux (it
doesn't work on AS/400, though...).

--
Jarek Zgoda
http://jpa.berlios.de/ | http://www.zgodowie.org/
Jul 18 '05 #12
Jarek Zgoda wrote:
You need to cause the main thread to wait on the child threads until
they've all finished, otherwise - as Peter Hickman just mentioned - your
script will terminate and all threads will end. Ten seconds on Google
found this:

http://www.faqts.com/knowledge_base/view.phtml/aid/1364


Are you sure? I keep observing that child threads continue its execution
until completion even if I do nothing in main thread. Consider this
example:


You are using the real thing - the highlevel threading.Thread class - not
the underlying thread.start_new_thread() function.
The threading module creates a _MainThread (a Thread subclass) instance that
registers itself as an atexit handler and waits for the other (non-demon)
Thread instances to terminate.

Failure-to-provide-the-source-code-will-ultimately-work-against
-companies-sitting-on-a-pile-of-crap-patents-ly yours

Peter

Jul 18 '05 #13
I just looked up for the keywords "python thread example" in google and few
useful links are retrieved. It seems that most of the examples are too complicated.

Scott David Daniels wrote:
Valkyrie wrote:
Thank you for your advice. I have quite seriously searched for it in fact, but
just without the luck on finding such article...


Can you suggest where this information might have shown up
that you would have found it? If so, you may want to submit
a documentation bug or patch request. That is, repay the
group by making it more likely that the next person with your
problem finds their answer on their own.

--Scott David Daniels
Sc***********@Acm.Org

Jul 18 '05 #14
So may I ask is threading do exactly the same in terms of functionalities as
those in thread?
Jarek Zgoda wrote:
Diez B. Roggisch wrote:
Even after printing "finished creating threads" all spawned threads
continue its execution up to final "thread nnnn is being destroyed", so
I think there's no need to take any special action (unless it's Python
on iSeries, buy this is another story) to wait for completion.

The OP used the module "thread", while you used "threading" and didn't
set
setDaemon(False). Then python waits until all threads are finished.

Well... I was bit too fast in replying. ;)
From the thread module docs:

When the main thread exits, it is system defined whether the other
threads
survive. On SGI IRIX using the native thread implementation, they
survive.
On most other systems, they are killed without executing try ... finally
clauses or executing object destructors.

So it looks that using threading should be preferred over using thread,
as presented example works identically on Windows and on linux (it
doesn't work on AS/400, though...).

Jul 18 '05 #15
Valkyrie wrote:
So may I ask is threading do exactly the same in terms of functionalities
as those in thread?


Yes, even more than thread. threading is considered the somewhat "better"
interface, as it exposes its funtionality in an objectorientied fashion -
and in this case, that's actually better :)
--
Regards,

Diez B. Roggisch
Jul 18 '05 #16

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

Similar topics

5
by: Richard P | last post by:
I need some help on timers. My app is asp.net 1.1 website running in a shared hosting environment with a third-party service provider. I currently request and cache 20 - 40 remote RSS feeds. When a...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
3
by: Jon Pope | last post by:
I've got a basic question about threading which I'm hoping someone here will be able to answer: Let's say my calling CallingClass has instantiated and started a worker thread. When my worker...
1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
18
by: Nick Z. | last post by:
I am writing a reusable class for logging. My goal is to make it as fast and as robust as possible, while keeping memory usage to the lowest. All members are static. For some reason I'm stuck on...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
2
by: SStory | last post by:
Here is the situation. I want to display Icons, Type of file etc from a file extension. Upon initial program load I may only need icons for certain files. But other operations will require...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
2
by: archana | last post by:
Hi all, I don't have much of knowledge about threading. I have on .net application where in main i am starting one secondary thread. like
4
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.