473,394 Members | 1,750 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,394 software developers and data experts.

Easier threading

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 4380
bartonc
6,596 Expert 4TB
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.Thread.
"""
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
Thanks once again. Don't know what I would do without you folks.
Apr 7 '07 #3
bartonc
6,596 Expert 4TB
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
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.Thread(group=None, target=quote.sendquote, name=None, connection, event)

correct?
Apr 8 '07 #5
bartonc
6,596 Expert 4TB
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.Thread(group=None, target=quote.sendquote, 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
no, It doesn't. I tried a few more variations But still nothing.
Apr 8 '07 #7
bartonc
6,596 Expert 4TB
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
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\lib\quotebot.py", line 5, in <module>
import quote, ontext
File "C:\Python25\lib\ontext.py", line 48
threading.Thread(group=None, target=sendquote, 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 Expert 4TB
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
nope, that doesn't produce any error messages or anything, the interpreter still is running and the bot is still working, minus the quote function.
Apr 9 '07 #11
bartonc
6,596 Expert 4TB
nope, that doesn't produce any error messages or anything, the interpreter still is running and the bot is still working, minus the quote function.
Is that quote module something that you wrote? If so can you post it? Or can I download it someplace?
Apr 9 '07 #12
actually the last code you gave me works, I had copied it wrong >.>

Thanks
Apr 12 '07 #13
bartonc
6,596 Expert 4TB
actually the last code you gave me works, I had copied it wrong >.>

Thanks
Oh, good. You really had me worried, there. Thanks for the update.
Apr 12 '07 #14
There is no real need to subclass threading.Thread.
"""
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.

Hi
I tried the above code and got an error about "global name 'self' is not defined". Also, I found that if I create a run method, the target is ignored.

Any ideas?

A
May 23 '07 #15
bartonc
6,596 Expert 4TB
Hi
I tried the above code and got an error about "global name 'self' is not defined". Also, I found that if I create a run method, the target is ignored.

Any ideas?

A
After much development, here is a class that contains a thread:
Expand|Select|Wrap|Line Numbers
  1. class USB_Device:
  2.     """This device model provide direct and background reads of the file handle.
  3.        The read() function chooses which to use based on the state of the Event().
  4.        All exceptions are passed up to the level above: direct reads use raise and
  5.        background reads put the error into the Queue().
  6.     """
  7.     def __del__(self):
  8.         self.Close()
  9.  
  10.     def __init__(self, devHandle, packetSize):
  11.         """USB_Device(C_TYPE_HANDLE, pyInt)"""
  12.         self.devHandle = devHandle
  13.         self.packetSize = packetSize
  14.  
  15.         self.recQueue = Queue()    # Queue for receiving packets
  16.         self.runEvent = threading.Event()
  17.         self.listener = None
  18.         self.running = False
  19.  
  20.     def StartBgRead(self):
  21.         """Create a background thread, set the runEvent and start the thread,
  22.            if one is not already running. Keep a reference for Stop() to use.
  23.         """
  24.         if not self.running:
  25.             self.listener = threading.Thread(target=self.BgRead, args=(self.devHandle,
  26.                                         self.recQueue, self.runEvent))
  27.             self.runEvent.set()
  28.             self.listener.start()
  29.             self.running = True
  30.  
  31.     def StopBgRead(self):
  32.         """if bg is running, clear the runEvent, join() the bg task
  33.            if it is alive and wait 1.5 seconds for it to die. Return
  34.            success (as the inverse of the state of the bg task)
  35.         """
  36.         state = True
  37.         if self.running:
  38.             self.runEvent.clear()
  39.             self.running = False
  40.             if self.listener is not None:
  41.                 if self.listener.isAlive():
  42.                     self.listener.join(1.5)
  43.                 state = not self.listener.isAlive()
  44.         return state
  45.  
  46.     def BgRead(self, devHandle, outQueue,  runEvent):
  47.         """Don't use the self variable. Threads don't have true (thread-safe)
  48.            access to shared py objects. I'd move it to the module scope, but
  49.            it's just so much easier to see here.
  50.         """
  51.         try:
  52.             # run until an error occures or the main thread signals here
  53.             while runEvent.isSet():
  54.                 thePacket = _read(devHandle)
  55.                 outQueue.put(thePacket)
  56.             # just here for debugging, really #
  57.             outQueue.put("Background terminated normally.")
  58.         except WindowsError, error:
  59.             # Put the error on the queue.
  60.             outQueue.put(error)
  61.             self.runEvent.clear()   # infrom main thread of exit
  62.  
  63.     def read(self):
  64.         """Background reads return None if there is nothing in the queue.
  65.            Errors in the background thread are put into the queue and the
  66.            thread exits.
  67.            Direct reads block until data has been read or an error occures.
  68.            Errors propogate up the call chain.
  69.            elif and else used for readablity, not program flow.
  70.         """
  71.         if self.runEvent.isSet():
  72.             try:
  73.                 return self.recQueue.get(timeout=.1)
  74.             except Empty:
  75.                 return
  76.         elif self.running:
  77.             # the background thread terminated
  78.             try:
  79.                 bgData = self.recQueue.get(timeout=.1)
  80.             except Empty: # should always get an Exception instance from the queue
  81.                 print 'empty q data:', bgData
  82.                 bgData = None
  83.             # Flush the queue:
  84.             while not self.recQueue.empty():
  85.                 try:
  86.                     self.recQueue.get(timeout=.1)
  87.                 except Empty:
  88.                     break
  89.             self.running = False
  90.             self.listener = None
  91.             return bgData
  92.         else:
  93.             try:
  94.                 return _read(self.devHandle)
  95.             except WindowsError:
  96.                 raise
  97.  
  98.     def write(self, aPacket):
  99.         """Bulk pipe write"""
  100.         nBytesToWrite = DWORD(sizeof(aPacket))
  101.         nBytesReturned = DWORD(0)
  102.  
  103.         success = WriteFile(self.devHandle,
  104.                             byref(aPacket),
  105.                             nBytesToWrite,
  106.                             byref(nBytesReturned),
  107.                             None)
  108.         if not success:
  109.             raise WinError()
  110.  
  111.         ##// If the packet size was an exact multiple of the USB packet
  112.         ##// size, we must make a final write call with no data
  113.         if (nBytesToWrite.value % self.packetSize) == 0:
  114.             success = WriteFile(self.devHandle,
  115.                                 DWORD(0), DWORD(0),
  116.                                 byref(nBytesReturned),
  117.                                 None)
  118.             if not success:
  119.                 raise WinError()
  120.  
  121.  
  122.     def ExpectPacketTypeAndID(self, pktType, pktId):
  123.         """"""
  124.         thePacket = self.read()
  125.         if (thePacket.mPacketType != pktType) or (thePacket.mPacketId != pktId):
  126.             msg = "Expected packet type %d, ID %d. Got type %d, ID %d"\
  127.                     %(pktType, pktId, thePacket.mPacketType, thePacket.mPacketId)
  128.             if thePacket.mDataSize:
  129.                 msg += " with data: %s" %str(thePacket.mData[:])
  130.             raise PyGarUSBError(msg)
  131.         return thePacket
  132.  
  133.     def Close(self):
  134.         self.StopBgRead()
  135.  
  136.  
  137.  
  138.  
  139. def _read(devHandle):
  140.     """Read async data until the driver returns less than the
  141.        max async data size, which signifies the end of a packet."""
  142.     pass
May 23 '07 #16
bartonc
6,596 Expert 4TB
Bumping thread because I can't figure out a lasting way to link to search results.
Jun 4 '07 #17

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

Similar topics

73
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...
65
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...
2
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...
77
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...
6
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...
2
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...
11
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...
17
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. ...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.