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

Python Threads+execute

Hello to everyone
I wont to create 2 threads in Python ( thread01, thread02 ), which doing the follows :
thread01, ask user to give a value to flag variable and write it to the shared memory of two threads
thread02, read from shared memory and check if the flag have a specific value .
The corresponding python code follows:
Expand|Select|Wrap|Line Numbers
  1. import time
  2. import thread
  3. import threading
  4. import Numeric, sys 
  5.  
  6. class newThread(threading.Thread):
  7.  
  8.     def __init__(self,name,threadID,sleeptime):
  9.         self.name=name
  10.         self.threadID=threadID
  11.         self.sleeptime=sleeptime
  12.  
  13.     def run(self,flag):
  14.         print time.ctime(time.time())
  15.         if self.threadID==2:
  16.             print "%s flag : "%(self.name,flag)
  17.             while flag!=100:
  18.                 time.sleep(self.sleeptime)
  19.             sys.exit()
  20.         elif self.threadID==1:
  21.             while 1:
  22.                 print "%s flag:"%(self.name, flag)
  23.                 flag=int(raw_input("Give a value at flag: "))
  24.                 time.sleep(self.sleeptime)
  25. flag=0
  26.  
  27. thread1=newThread("Thread No:1",1,5)
  28. thread2=newThread("Thread No:2",2,15)
  29.  
  30. thread1.run(flag)
  31. thread2.run(flag)
  32.  
  33. while 1:
  34.     pass
  35.  
  36. thread1.exit()
  37. thread2.exit()
When I call it from python 2.5(Linux kubuntu 7.04), thread02 never take the CPU and only thread01 execute. Why does this happen ? The 2 threads does not run concurrently in double core processors?Why does not thread02 take cpu when thread 1 sleep? the Is there any solution of this problem?
Sep 24 '07 #1
5 3639
bartonc
6,596 Expert 4TB
First, let's get you using [code] tags. Instructions are on the right hand side of the page when you "start a discussion" with the heading "POSTING GUIDELINES" or when you reply, with the heading "REPLY GUIDELINES". Thanks.

I whipped up an example a few days ago. I generally use the simpler method that doesn't require subclassing of threading.thread. I'll look for that post and get back soon.
Sep 24 '07 #2
bartonc
6,596 Expert 4TB
For an example in interlocking threads from the main thread, see this thread.
For concurrency, see Parallel Python.

I wouldn't try to get user interaction from a thread that is not the main thread. Threads do tend to print fairly reliably, though.

sleep() is not guaranteed to release the processor. In some systems it works, but I've seen it fail.
Sep 24 '07 #3
Hello bartonc
Thank you for your advices, I appreciate it much, but still I have some questions. Does Parallel Python solve problems for concurrent execution of threads? As I read, Parallel Python used to solve problems for parallelism between processors,like MPI does. But my problem refer to parallel execution of 2 threads at the same processor, which execute different functions. Threads use shared memory communication so both of them can read and write to the variable flag. If first thread write to the flag the value of 200 then the second thread, according to the check which execute must end the program. The only way to make this is to change periodically the flow control of CPU . At first time, first thread must have the control and then the second. Do you have any ideas . Is there any function except sleep() , which stop the execution of one thread and pass the flow control to the other.
Thanks a lot , again.
Sep 25 '07 #4
bartonc
6,596 Expert 4TB
Hello bartonc
Thank you for your advices, I appreciate it much, but still I have some questions. Does Parallel Python solve problems for concurrent execution of threads? As I read, Parallel Python used to solve problems for parallelism between processors,like MPI does. But my problem refer to parallel execution of 2 threads at the same processor, which execute different functions. Threads use shared memory communication so both of them can read and write to the variable flag. If first thread write to the flag the value of 200 then the second thread, according to the check which execute must end the program. The only way to make this is to change periodically the flow control of CPU . At first time, first thread must have the control and then the second. Do you have any ideas . Is there any function except sleep() , which stop the execution of one thread and pass the flow control to the other.
Thanks a lot , again.
They will run in a multi-threaded environment and share cpu time if they are will written. The main rule to remember is that

Threads must use thread-safe means of communication! They may not share main thread memory.

By using a Queue.Queue() threads may wait for one another in a cooprative way.
threading.Event() is another technique that will work. There are also Locks, RLocks and Semaphore Objects at your disposal.
Sep 25 '07 #5
Thank you a lot, your advice was helpful. If I find any other relative question , i will write again .
Sep 27 '07 #6

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

Similar topics

3
by: Krisztian Kepes | last post by:
Hi ! I want to start many py programs - with other parameters. Because the programs have many to-do, I don't want to wait for them. In Delphi it is like this: >>>>>>>>>>>>>>>
37
by: Carlos Ribeiro | last post by:
Oh well. A mailing list is not the most appropriate place for rants (a blog is better), but it's still better than keeping it for myself. I'm frustrated. My search for a good IDE to support my...
3
by: Travis Berg | last post by:
I'm running into a problem when trying to perform a callback to a Python function from a C extension. Specifically, the callback is being made by a pthread that seems to cause the problem. If I...
4
by: Russell Stuart | last post by:
I always believed .Net monitors were a superset of the Java synchronization mechanisms. The code below produces both "Thread 1" and "Thread 2" lines, implying that two threads are both executing...
2
by: Kenneth McDonald | last post by:
I'm not trying to persuade my company to offer Python as a scripting language for their product, but I am trying to give them examples of things that Python can do easily that cannot be done easily...
3
by: Wolfram | last post by:
I have a problem with displaying errors in an embedded situation. The "main program" I want to embed Python into is a windows, MFC, non-console, C++ application. My issue is that I have not been...
2
by: Gabriel Genellina | last post by:
En Tue, 13 May 2008 06:42:13 -0300, Astan Chee <stanc@al.com.auescribió: I'm confused trying to understand your requirements too. "run a class?" Threads execute code, it's easier to think of...
4
by: Ognjen Bezanov | last post by:
Hello All, I am a third year computer science student and I'm the process of selection for my final year project. One option that was thought up was the idea of implement my own version of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.