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

threading problems

main problem is i dont really understand threads, and at the moment they are my only option because im working on windows and it doesnt support fork processes.ran this program and all i got was a random integer ( 348 )

import thread

mutex=thread.allocate_lock()
f=open('newText.txt.','w')

def counter2(ID):
mutex.acquire()
f.write(ID,'\nNumbers are ')
for i in range(5): f.write(i)
f.write('\n')
mutex.release()


thread.start_new(counter2,(1,))



...and and all i got was an integer which wasnt even supposed to appear (i understand that each thread when started they return a value which is supposed to be ignored), the file cointains nothing
Jan 1 '08 #1
1 985
ilikepython
844 Expert 512MB
main problem is i dont really understand threads, and at the moment they are my only option because im working on windows and it doesnt support fork processes.ran this program and all i got was a random integer ( 348 )

import thread

mutex=thread.allocate_lock()
f=open('newText.txt.','w')

def counter2(ID):
mutex.acquire()
f.write(ID,'\nNumbers are ')
for i in range(5): f.write(i)
f.write('\n')
mutex.release()


thread.start_new(counter2,(1,))



...and and all i got was an integer which wasnt even supposed to appear (i understand that each thread when started they return a value which is supposed to be ignored), the file cointains nothing
First of all please us code tags.

In your counter2 function there are a couple errors. Why are you passing 2 arguements to f.write()? Why are you passing an integer to f.write()? You should only pass a single string to that function.

Also, you shouldn't exit the main thread until all your chil threads have exited. You could do something like this:
Expand|Select|Wrap|Line Numbers
  1. import thread
  2. import time
  3.  
  4. mutex=thread.allocate_lock()
  5. f=open('newText.txt.','w')
  6. done = false
  7.  
  8. def counter2(ID):
  9.     mutex.acquire()
  10.     f.write(ID,'\nNumbers are ')
  11.     for i in range(5):
  12.         f.write(i)
  13.     f.write('\n')
  14.     mutex.release()
  15.     done = true
  16.  
  17.  
  18. thread.start_new(counter2,(1,))
  19.  
  20. while not done:
  21.     time.sleep(0.2)
  22.  
That might not be necessary though depending on what you are doing.
Jan 1 '08 #2

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

Similar topics

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...
0
by: Holger Joukl | last post by:
Hi there, I have severe problems running python 1.5.2 (yes I know, but this is a productive environment; cannot switch to py 2.3.3 until later this year...) on several multi-processor sun...
13
by: Varun | last post by:
Hi Friends, Department of Information Technology, Madras Institute of Technology, Anna University, India is conducting a technical symposium, Samhita. As a part of samhita, an Online Programming...
1
by: Ognjen Bezanov | last post by:
Hi, all Thanks all of you who helped me with the threading and queues issue. I am trying to get it working but I am having problems. When I try to run the following: cmddata =...
18
by: Frank Rizzo | last post by:
Hello, I have a class with all static methods that is called by multiple threads. I was wondering what effect that has on the competing threads. Does Thread2 have to wait until Thread1 is done...
13
by: John | last post by:
I've got some reasonably complex business logic in my C# code, in a class called by a ASP.NET page. This takes around 3-4 seconds to execute. It's not dependent on SQL calls or anything like that....
8
by: cj | last post by:
I need more information to read about threading in Visual Basic. Hopefully something linear w/o too many links to get lost in. I've been to...
2
by: WXS | last post by:
When I see things in .NET 2.0 like obsoletion of suspend/resume because of the public reason MS gives of they think people are using them inappropriately.. use mutex, monitor and other...
15
by: WXS | last post by:
When I see things in .NET 2.0 like obsoletion of suspend/resume because of the public reason MS gives of they think people are using them inappropriately.. use mutex, monitor and other...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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,...
0
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...

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.