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

Home Posts Topics Members FAQ

Noob can't make threads work

I am trying to start two threads to do some time consuming work. This is my
first stab at threading, and it isn't working as I expect. Instead of the
threads starting when I call start(), they seem to run the target code as
part of the constructor call.

Here is my test code...

#!/usr/bin/python

import time
import threading

def fiddle():
for n in range(3):
print n
time.sleep(1)

print 'Creating threads...'
t1 = threading.Threa d(target=fiddle ())
t2 = threading.Threa d(target=fiddle ())
print 'Starting threads...'
t1.start()
t2.start()
I was expecting output like this:

Creating threads...
Starting threads...
0
0
1
1
2
2

but I get this instead:

Creating threads...
0
1
2
0
1
2
Starting threads...

This is in python 2.3 on Linux and also python 2.4 on XP.
Either threading in Python is badly broken, or I'm missing something
fundamental. I know which is most likely, but I can't figure it out.

Could someone point me in the right direction, plese?

TIA
Steve
Jul 18 '05 #1
4 1386
On 2005-03-01, Steve Horsley <sh***@the.moon > wrote:
I am trying to start two threads to do some time consuming work. This is my
first stab at threading, and it isn't working as I expect. Instead of the
threads starting when I call start(), they seem to run the target code as
part of the constructor call.

Here is my test code...

#!/usr/bin/python

import time
import threading

def fiddle():
for n in range(3):
print n
time.sleep(1)

print 'Creating threads...'
t1 = threading.Threa d(target=fiddle ())
t2 = threading.Threa d(target=fiddle ())
t1 = threading.Threa d(target=fiddle )
t2 = threading.Threa d(target=fiddle )

print 'Starting threads...'
t1.start()
t2.start()
I was expecting output like this:

Creating threads...
Starting threads...
0
0
1
1
2
2

but I get this instead:

Creating threads...
0
1
2
0
1
2
Starting threads...

This is in python 2.3 on Linux and also python 2.4 on XP.
Either threading in Python is badly broken, or I'm missing something
fundamental. I know which is most likely, but I can't figure it out.

Could someone point me in the right direction, plese?

TIA
Steve

--
Grant Edwards grante Yow! We just joined the
at civil hair patrol!
visi.com
Jul 18 '05 #2
Grant Edwards wrote:
On 2005-03-01, Steve Horsley <sh***@the.moon > wrote:
I am trying to start two threads to do some time consuming work. This is my
first stab at threading, and it isn't working as I expect. Instead of the
threads starting when I call start(), they seem to run the target code as
part of the constructor call.

Here is my test code...

#!/usr/bin/python

import time
import threading

def fiddle():
for n in range(3):
print n
time.sleep(1)

print 'Creating threads...'
t1 = threading.Threa d(target=fiddle ())
t2 = threading.Threa d(target=fiddle ())

t1 = threading.Threa d(target=fiddle )
t2 = threading.Threa d(target=fiddle )

Doh! Slap that forehead!

Of course, I was calling fiddle() and using the
return value (None) as the target argument. And
a Thread with a target of None does nothing
when start()ed.

Many thanks.
Steve
Jul 18 '05 #3
On 2005-03-01, Steve Horsley <sh***@the.moon > wrote:
t1 = threading.Threa d(target=fiddle ())
t2 = threading.Threa d(target=fiddle ())


t1 = threading.Threa d(target=fiddle )
t2 = threading.Threa d(target=fiddle )

Doh! Slap that forehead!

Of course, I was calling fiddle() and using the
return value (None) as the target argument. And
a Thread with a target of None does nothing
when start()ed.


Been there, done that.

--
Grant Edwards grante Yow! BELA LUGOSI is my
at co-pilot...
visi.com
Jul 18 '05 #4
Grant Edwards wrote:
On 2005-03-01, Steve Horsley <sh***@the.moon > wrote:

t1 = threading.Threa d(target=fiddle ())
t2 = threading.Threa d(target=fiddle ())

t1 = threading.Threa d(target=fiddle )
t2 = threading.Threa d(target=fiddle )

Doh! Slap that forehead!

Of course, I was calling fiddle() and using the
return value (None) as the target argument. And
a Thread with a target of None does nothing
when start()ed.

Been there, done that.

And, speaking of T-shirts, time for this week's reminder that PyCon is
now only three weeks away!

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #5

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

Similar topics

3
1760
by: Sebastian Meyer | last post by:
Hi Newsgroup, i have some problems with using threads and signals in one program. In my program i have three threads running, one for checking a directory at a specified interval to see if new data arrived, one waiting for work and the main thread. My problem is the following: I want to shutdown the program via a signal, so i used the signal module. I know, that signal handling mus be done by the main thread, the signal
1
1814
by: davestrike | last post by:
I am a noob to sql and asp programming. I am working on a db for the gaming squad I am a member of. One of the pages I created is a roster list of all the squad members. Part of this roster is listing each member's email address. What several people have asked of me is to make it so the email addresses can be clicked on to open their email programs, just as html allows the mailto function to work. Here is a copy of the coding I am...
34
10843
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; public class A {
6
5405
by: Alexander Walker | last post by:
Hello Is it inefficient to create an application that has many threads that individually may do a small amount of work over a given period of time as opposed to an application that has a smaller number of threads that do a larger amount of work over a given time period here is an oversimplified example application 1 has 4 threads
0
1489
by: AndyW | last post by:
Hey folks. I am trying to get a soap wsdl service working and have a bit of a noob php programming question for it. I'm using PHP 5.x btw. I have written a soap server that contains a single method in the form: SayHelloWorld( $name ) { return "Hello " .$name; };
1
1978
by: Japskunk | last post by:
I am having trouble updating a SQL table through the GridView "Auto" Enable Edit Feature... I am connecting to a SQL 2000 Server with a SQLDataSource I have created the Update Query in the Command and Parameter Editor setup when I created my SQLadapter, along with Select and Delete. Here is the Update Command: UPDATE TBLUPSData SET UPSModel = @original_UPSModel, UPSLocation = @original_UPSLocation, UPSDeviceAttached =...
3
1722
by: Animesh K | last post by:
Hello All: I am new to Php, but working my way out slowly. I saw some polling or voting scripts on the net and all of them use MySQL database with it. I was wondering if a file can be used (to save use of a database, I have only 10-databases from godaddy and I cannot use one for each poll). It seems that this can be done (using file instead of a DB), but I don't want to re-invent the wheel. So if someone has a suggestion, please help...
4
2112
by: larry | last post by:
Ok I'm a Python noob, been doing OK so far, working on a data conversion program and want to create some character image files from an 8-bit ROM file. Creating the image I've got down, I open the file and use TK to draw the images... but 1) It does not seem to end (running in IDLE), I have to kill the process to retry it seems tkinter does not close(?)
1
2539
by: Fluffy654 | last post by:
First off I am a complete noob when it comes to doing anything with servers. I'm just beginning to learn today because I need to start adding SSI to my websites. I apologise in advance if I am making a really obvious mistake. To test this, I have successfully installed apache and have read the official documentation on how to get SSI functionality working. Unfortunately, I can't get it to work at all. After reading tons of threads on this...
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9776
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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
10401
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...
0
9554
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7110
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();...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
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
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.