471,310 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 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 941
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

Post your reply

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

Similar topics

65 posts views Thread by Anthony_Barker | last post: by
reply views Thread by Holger Joukl | last post: by
13 posts views Thread by Varun | last post: by
1 post views Thread by Ognjen Bezanov | last post: by
18 posts views Thread by Frank Rizzo | last post: by
13 posts views Thread by John | last post: by
8 posts views Thread by cj | last post: by
2 posts views Thread by WXS | last post: by
15 posts views Thread by WXS | last post: by
126 posts views Thread by Dann Corbit | last post: by
reply views Thread by rosydwin | last post: by

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.