473,881 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem in threading

I have written a code to figure out the difference in excecution time
of a func before and after using threading...

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2.  
  3. import threading
  4. import time
  5. loops = [5000,5000]
  6.  
  7. def loop(self, nsec):
  8. for i in range(1,nsec):
  9. t=i*5000
  10. s=t/10*15555
  11.  
  12. def main():
  13. threads = []
  14. nloops = [0,1]
  15. for i in nloops:
  16. print '\nSpawning Thread',i,' value: ',loops[i]
  17. t = threading.Thread(target=loop,args=(i, loops[i]))
  18. threads.append(t)
  19.  
  20. for i in nloops: # start threads
  21. threads[i].start()
  22.  
  23. for i in nloops: # wait for all
  24. threads[i].join
  25.  
  26. if __name__ == '__main__':
  27. start = time.clock()
  28. loop(1,5000)
  29. print 'Time Taken: ', time.clock()-start
  30. start = time.clock()
  31. main()
  32. print 'Time Taken: ', time.clock()-start
  33.  
Some times this code executes and some times it hangs to death :o(
Am I am doing something wrong?? Also the difference of time is not much...
How do we best optimize our task by using threads... please help...

Thanks and Regards,
Gurpreet Singh
Jul 18 '05
12 1765
That's an OT topic. :=)

There were lots of discussions about this topic in the old days. No need
to dive into it again.

Windows context switching overhead is very high. You would be lucky to get
it down to the mid-30ms. OS/2 can get it down to less then 10. And for
OS/2, thread swithing time is only a few machine instructions... .OT.OT.
"David Bolen" <db**@fitlinxx. com> wrote in message
news:ua******** ***@fitlinxx.co m...
"It's me" <it***@yahoo.co m> writes:
It depends on what "help" means to you. Both Windows and Unix (and it's variances) are considered "thread-weak" OSes. So, using thread will come with some cost. The long gone IBM OS/2 is a classic example of a
"thread-strong" OS.

(...)

Interesting - can you clarify what you perceive as the differences
between a thread-weak and thread-strong OS? If given the choice, I
would probably refer to Windows (at least NT based systems, let's
ignore 9x) as thread-strong, and yes, often think of Windows as
preferring thread based solutions, while Unix would often prefer
process based.

Windows is far more efficient at handling large numbers of threads
than it is processes, with much less overhead and there is lots of
flexibility in terms of managing threads and their resources. Threads
are first class OS objects at the kernel and scheduler level (waitable
and manageable).

I can't think of anything offhand specific that OS/2 did with respect
to threads that isn't as well supported by current Win32 systems.

-- David

Jul 18 '05 #11
Peter Hansen <pe***@engcorp. com> writes:
Mike Meyer wrote:
Python's threading models is pretty primitive. You get the C
model (which is error-prone), the Java model (in 2.4, and also
error-prone), or Queues.

Can you please expand on your words above? I have no idea
what you are talking about with the "Java model" and your
implication that it is something new in Python 2.4. As
far as I know, nothing significant with respect to threads
has changed in Python recently.


See <URL: http://docs.python.org/lib/module-threading.html > which
clearly has the words "New in 2.4" on it. It also says that the
threading module is loosely based on the Java model. I may have
misinterpreted what the "New in 2.4" applies to, which means that the
comment about the Java model being new in 2.4 would be wrong.

As for error-prone, both models require the programmer to always
obtain locks around critical sections, and to obtain them in an order
that prevents deadlocks, and release them in an order that prevents
starvation.

There are threading models that don't require the programmer to get
the locking right. In those, you basically declare an object as
running on a different thread, and the compiler provides the
appropriate locks for each method. I went looking for a reference to
one such method, but couldn't turn it up, partly because I don't
recall whether I saw it on usenet or a mail list.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #12
Mike Meyer wrote:
See <URL: http://docs.python.org/lib/module-threading.html > which
clearly has the words "New in 2.4" on it. It also says that the
threading module is loosely based on the Java model. I may have
misinterpreted what the "New in 2.4" applies to, which means that the
comment about the Java model being new in 2.4 would be wrong.


You have misinterpreted that. The "New in 2.4" part is not
so much "on" that page as it is "in" it, and specifically
it is right in the section on the new-to-2.4 class "local".

The rest of the threading module is basically unchanged since
a number of versions ago, but thanks for your clarification. :-)

-Peter
Jul 18 '05 #13

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

Similar topics

1
2458
by: D. Shifflett | last post by:
Hi all, I am having trouble with a program that ran fine on Python 2.0 (#0, Mar 1 2001, 01:47:55) on linux2 but will not work on Python 2.3.2 (#1, Oct 8 2003, 17:33:47) on linux2
2
2997
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def get_all_files(path): """return all files of folder path, scan with subfolders
2
1423
by: hecklar | last post by:
This is my first time posting here, so i apologize if i'm posting in the wrong subgroup or whatever, but here goes... I’m having a problem with threading and events (permissions?) in a VB.net Windows application (background service). I’m trying to write an application that processes files, launching a new thread for each file that is dropped into a certain folder. Now, the application works like a charm on my Win2000 machine, but...
2
1340
by: peleme | last post by:
Hi, Here is a code example to visualize my problem. ---------------------------------------------------------------------------------- import thread import threading from time import sleep def a():
0
285
by: fiefie.niles | last post by:
I am having problem with thread. I have a Session class with public string variable (called Message) that I set from my Main program. In the session class it checks for the value of Message while inside it's "read loop" waiting for data from the client. I find that many times while inside the "read loop" it missed many of the value that was assigned to the public Message variable. For example, the main program send number 1 thru 100, but...
9
3278
by: esakal | last post by:
Hello, I'm programming an application based on CAB infrastructure in the client side (c# .net 2005) Since my application must be sequencally, i wrote all the code in the UI thread. my problem occurs when i try to show a progress bar. The screen freezes. I know i'm not the first one to ask about it. but i'm looking
12
2306
by: Justin | last post by:
I can attach my code if anyone wants to see it however I'll try to ask my question with some mark up code first. I'm having a problem terminating my process while using DoEvents. For example: Button.text = start If button.text = start then button.text = stop
5
6941
by: CCLeasing | last post by:
For an application I'm creating I want to create a 'fake' progress bar. By fake I mean a progress bar that looks like it's doing something but actually isn't. I know philosophically this isn't sound. But my little app is a 'fake' app and is designed to look like another - hence this seeming crazy situation of needing to fake a progess bar. PROBLEM.
2
1902
by: =?Utf-8?B?TWljaGFlbCBXLg==?= | last post by:
Dear ng, i have developed a winforms application with vs2005. An progress from with a animation and a timer is shown while the application is working. The trouble is, that often a ThreadAbordException occurs and i can not identify whats the reason is.
0
1097
by: Bieniu | last post by:
I have DataList control on my own contro land it is bind to SqlDataSource control. My problem is that DataList is getting needed data twice what is for me very strange. I have some code in OnSelected event of SqlDataSource, and i was suprised when it was fired twice for every page loading. His is code of datalist: <tr> <td style="width: 5%;"> </td> <td colspan="3">
0
9928
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
11100
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10718
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10402
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7953
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7111
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4597
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
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3225
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.