472,992 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 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 980
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.