473,574 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Easier threading

56 New Member
Expand|Select|Wrap|Line Numbers
  1. import threading
  2. import time
  3.  
  4. class ThreadOne ( threading.Thread ):
  5.     def run ( self ):
  6.         print 'Thread', self.getName(), 'started.'
  7.         time.sleep ( 5 )
  8.         print 'Thread', self.getName(), 'ended.'
  9. class ThreadTwo ( threading.Thread ):
  10.     def run ( self ):
  11.         print 'Thread', self.getName(), 'started.'
  12.         time.sleep ( 5 )
  13.         print 'Thread', self.getName(), 'ended.'
  14. thingOne = ThreadOne()
  15. thingOne.start()
  16. thingTwo = ThreadTwo()
  17. thingTwo.start()
This is the example I keep finding on threading, The problem is, I don't know how many threads I need. Basically, I was wondering if there was a command to start a new thread that runs a command already made, maybe like:

startNewThread( MyFunction(Arg1 , Arg2, Arg3))

Is there such a function, Or Ideas on how I can make one? Thanks again.
Apr 7 '07 #1
16 4389
bartonc
6,596 Recognized Expert Expert
Expand|Select|Wrap|Line Numbers
  1. import threading
  2. import time
  3.  
  4. class ThreadOne ( threading.Thread ):
  5.     def run ( self ):
  6.         print 'Thread', self.getName(), 'started.'
  7.         time.sleep ( 5 )
  8.         print 'Thread', self.getName(), 'ended.'
  9. class ThreadTwo ( threading.Thread ):
  10.     def run ( self ):
  11.         print 'Thread', self.getName(), 'started.'
  12.         time.sleep ( 5 )
  13.         print 'Thread', self.getName(), 'ended.'
  14. thingOne = ThreadOne()
  15. thingOne.start()
  16. thingTwo = ThreadTwo()
  17. thingTwo.start()
This is the example I keep finding on threading, The problem is, I don't know how many threads I need. Basically, I was wondering if there was a command to start a new thread that runs a command already made, maybe like:

startNewThread( MyFunction(Arg1 , Arg2, Arg3))

Is there such a function, Or Ideas on how I can make one? Thanks again.
There is no real need to subclass threading.Threa d.
"""
class Thread( group=None, target=None, name=None, args=(), kwargs={})

This constructor should always be called with keyword arguments. Arguments are:
group should be None; reserved for future extension when a ThreadGroup class is implemented.

target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

name is the thread name. By default, a unique name is constructed of the form ``Thread-N'' where N is a small decimal number.

args is the argument tuple for the target invocation. Defaults to ().

kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.

If the subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init_ _()) before doing anything else to the thread.
"""
Expand|Select|Wrap|Line Numbers
  1. def myFunc(arg1, arg2):
  2.     print 'Thread', self.getName(), 'started.'
  3.     print arg1, arg2
  4.     time.sleep ( 5 )
  5.     print 'Thread', self.getName(), 'ended.'
  6.  
  7. t1 = threading.Thread(group=None, target=myFunc, name=None, 1, 2)
  8. t2 = threading.Thread(group=None, target=myFunc, name=None, 'a', 'b')
  9. t1.start()
  10. t2.start()
is what you're looking for.
Apr 7 '07 #2
William Manley
56 New Member
Thanks once again. Don't know what I would do without you folks.
Apr 7 '07 #3
bartonc
6,596 Recognized Expert Expert
Thanks once again. Don't know what I would do without you folks.
You are welcome. That's what this site is all about!
Apr 7 '07 #4
William Manley
56 New Member
Hmm, I'm having a little trouble with this.

My function I need executed is: quote.sendquote (connection, event)

so, the command should look like:
threading.Threa d(group=None, target=quote.se ndquote, name=None, connection, event)

correct?
Apr 8 '07 #5
bartonc
6,596 Recognized Expert Expert
Hmm, I'm having a little trouble with this.

My function I need executed is: quote.sendquote (connection, event)

so, the command should look like:
threading.Threa d(group=None, target=quote.se ndquote, name=None, connection, event)

correct?
I've never tried to use a bound method as a thread target, but it looks OK.
Does it work?
Apr 8 '07 #6
William Manley
56 New Member
no, It doesn't. I tried a few more variations But still nothing.
Apr 8 '07 #7
bartonc
6,596 Recognized Expert Expert
no, It doesn't. I tried a few more variations But still nothing.
Paste some error messages so that we can see what you are getting.

If quote is a class, perhaps you need an instance. If it is a module try
Expand|Select|Wrap|Line Numbers
  1. from quote import sendquote
  2. threading.Thread(group=None, target=sendquote, name=None, connection, event)
  3.  
Posting code is very helpful in these situations.
Apr 8 '07 #8
William Manley
56 New Member
Expand|Select|Wrap|Line Numbers
  1. from quote import sendquote
  2. threading.Thread(group=None, target=sendquote, name=None, connection, event)
  3.  
this code produces the same error as what I was doing before.

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win
32
Type "help", "copyright" , "credits" or "license" for more information.
>>> import quotebot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python25\li b\quotebot.py", line 5, in <module>
import quote, ontext
File "C:\Python25\li b\ontext.py", line 48
threading.Threa d(group=None, target=sendquot e, name=None, connection, event)

SyntaxError: non-keyword arg after keyword arg
>>>
As for the code, Its quite a long script, but using quote.sendquote (connection, event) does work.
Apr 9 '07 #9
bartonc
6,596 Recognized Expert Expert
Expand|Select|Wrap|Line Numbers
  1. from quote import sendquote
  2. threading.Thread(group=None, target=sendquote, name=None, connection, event)
  3.  
this code produces the same error as what I was doing before.



As for the code, Its quite a long script, but using quote.sendquote (connection, event) does work.
That helps a lot! Try
Expand|Select|Wrap|Line Numbers
  1. from quote import sendquote
  2. threading.Thread(None, sendquote, None, connection, event)
Apr 9 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

73
7946
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities as a standard portable part of the core language, the LISP package, and the java.lang package, respectively. Both have big integers, although only...
65
6695
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the language's 30+ year evolution. What to you think python largest compromises are? The three that come to my mind are significant whitespace,...
2
2961
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
77
5232
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go...
6
555
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service and the final process(3) gets called. what am I doing wrong with the threading? by the way Directory.GetFiles(IncomingXMLPath1).Length is some...
2
2238
by: Vjay77 | last post by:
In this code: Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not (Me.downloadUrlTextBox.Text = "") Then Me.outputGroupBox.Enabled = True Me.bytesDownloadedTextBox.Text = "" Me.totalBytesTextBox.Text = ""
11
5004
by: Paul Sijben | last post by:
I am stumped by the following problem. I have a large multi-threaded server accepting communications on one UDP port (chosen for its supposed speed). I have been profiling the code and found that the UDP communication is my biggest drain on performance! Communication where the client and the server are on the same machine still takes 300ms...
17
6399
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. Note, the Windows task manager shows 2 CPUs on the Performance tab with hyper-threading is turned on. Both Python 2.3.5 and 2.4.3 (downloaded from...
7
2363
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has finished. I am trying to follow this example : http://www.codeproject.com/cs/miscctrl/progressdialog.asp But although the messages still get...
0
7803
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7720
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
8044
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
8096
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...
0
6451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5618
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
5299
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
3749
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1337
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.