473,569 Members | 2,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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",para m

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

Thanks in advance
Jul 18 '05 #1
15 1575
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",para m

import thread
for i in range(5): # start five threads passing i to each one
thread.start_ne w_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",para m

import thread
for i in range(5): # start five threads passing i to each one
thread.start_ne w_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",para m
.... import thread
for i in range(5): # start five threads passing i to each one .... thread.start_ne w_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_ne w_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",para m

import thread
for i in range(5): # start five threads passing i to each one
thread.start_ne w_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",para m
...
import thread
for i in range(5): # start five threads passing i to each one
... thread.start_ne w_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_ne w_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",para m

import thread
for i in range(5): # start five threads passing i to each one
thread.start_ne w_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***********@A cm.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

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

Similar topics

5
2122
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 user requests the page, the app first tries to retrieve a feed from cache, if the feed has expired, it goes off and request the file from the web....
11
4234
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 threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do...
3
1985
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 thread has completed, it fires an event which CallingClass has an event handler listening for. My question is this: in which thread is that event...
1
2832
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 remote server and display this info on the UI. From doing some research, I know that the way my implementation works today is not thread-safe, because...
18
2038
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 the following design question. Obviously you need a stream to write the log file. Should this stream be created every time the log needs to be...
10
3426
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 somewhere that each folder under the "web site" is compiled in separate assembly. I however, did not find that the "web site" creation in vs.net...
2
1650
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 showing all filetypes and icons. I have an object that has extension, desc (like Word Document) and then icon, which is nothing to start with. I...
10
3692
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 Error in '/QuickStartv20' Application. -------------------------------------------------------------------------------- Configuration Error...
2
1414
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
3593
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 client and asynchronous socket server example code provided in the .NET framework developers guide is a great start but I have not dealt with...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
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 we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.