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

Is it possible to run two "while 1:" loops in two threadings respectively?

Hi,
I would like to combine two python applications into a single one
with two threadings. Both of them have a "while 1:" loop respectively.
For example, one application is to monitoring serial port 'com1' and
another application is a TCP/IP server which has used threadings
already. I write the following demo code but it does not work right.
It stays in the first "while 1:" and never thingOne.start(). The
second threading never be started.
Any ideas?

Thanks a lot.

Ouyang
################################################## ###############
import threading, time
class serial_port_com1:
def spc(self):
i = 0
while 1:
time.sleep(5)
print "%d: hello, I am here in spc()"%i
i += 1

class TCP_IP:
def tcpip(self):
i = 0
while 1:
time.sleep(5)
print "%d: hello, I am here in tcpip()"%i
i += 1

class ThreadOne ( threading.Thread ):
def run ( self ):
print 'Thread', self.getName(), 'started.'
time.sleep ( 5 )
print 'Thread', self.getName(), 'ended.'

class ThreadTwo ( threading.Thread ):
def run ( self ):
print 'Thread', self.getName(), 'started.'
thingOne.join()
print 'Thread', self.getName(), 'ended.'

if __name__=="__main__":
spc = serial_port_com1()
tcpip = TCP_IP()
thingOne = ThreadOne(target=spc.spc())
thingOne.start()
thingTwo = ThreadTwo(target=tcpip.tcpip())
thingTwo.start()

Jul 17 '07 #1
2 1107
zxo102 schrieb:
Hi,
I would like to combine two python applications into a single one
with two threadings. Both of them have a "while 1:" loop respectively.
For example, one application is to monitoring serial port 'com1' and
another application is a TCP/IP server which has used threadings
already. I write the following demo code but it does not work right.
It stays in the first "while 1:" and never thingOne.start(). The
second threading never be started.
Any ideas?

Thanks a lot.

Ouyang
################################################## ###############
import threading, time
class serial_port_com1:
def spc(self):
i = 0
while 1:
time.sleep(5)
print "%d: hello, I am here in spc()"%i
i += 1

class TCP_IP:
def tcpip(self):
i = 0
while 1:
time.sleep(5)
print "%d: hello, I am here in tcpip()"%i
i += 1

class ThreadOne ( threading.Thread ):
def run ( self ):
print 'Thread', self.getName(), 'started.'
time.sleep ( 5 )
print 'Thread', self.getName(), 'ended.'

class ThreadTwo ( threading.Thread ):
def run ( self ):
print 'Thread', self.getName(), 'started.'
thingOne.join()
print 'Thread', self.getName(), 'ended.'

if __name__=="__main__":
spc = serial_port_com1()
tcpip = TCP_IP()
thingOne = ThreadOne(target=spc.spc())
thingOne.start()
thingTwo = ThreadTwo(target=tcpip.tcpip())
thingTwo.start()
There are several problems here. First of all, one either subclasses
Thread and implements run - then your code should look like this:
class ThreadTwo(Thread):
def run(self):
tcpip.tcpip()
Or you don't subclass Thread and pass a target. But that target must be
a function. You don't pass a function, you call it!!

Look at this:

Thread(target=tcpip.tcpip)

Note the missing parentheses!

Apart from that, you should seriously consider applying a consistent
naming style to your code.
Diez
Jul 17 '07 #2
On 7 17 , 3 01 , "Diez B. Roggisch" <de...@nospam.web.dewrote:
zxo102 schrieb:


Hi,
I would like to combine two python applications into a single one
with two threadings. Both of them have a "while 1:" loop respectively.
For example, one application is to monitoring serial port 'com1' and
another application is a TCP/IP server which has used threadings
already. I write the following demo code but it does not work right.
It stays in the first "while 1:" and never thingOne.start(). The
second threading never be started.
Any ideas?
Thanks a lot.
Ouyang
################################################## ###############
import threading, time
class serial_port_com1:
def spc(self):
i = 0
while 1:
time.sleep(5)
print "%d: hello, I am here in spc()"%i
i += 1
class TCP_IP:
def tcpip(self):
i = 0
while 1:
time.sleep(5)
print "%d: hello, I am here in tcpip()"%i
i += 1
class ThreadOne ( threading.Thread ):
def run ( self ):
print 'Thread', self.getName(), 'started.'
time.sleep ( 5 )
print 'Thread', self.getName(), 'ended.'
class ThreadTwo ( threading.Thread ):
def run ( self ):
print 'Thread', self.getName(), 'started.'
thingOne.join()
print 'Thread', self.getName(), 'ended.'
if __name__=="__main__":
spc = serial_port_com1()
tcpip = TCP_IP()
thingOne = ThreadOne(target=spc.spc())
thingOne.start()
thingTwo = ThreadTwo(target=tcpip.tcpip())
thingTwo.start()

There are several problems here. First of all, one either subclasses
Thread and implements run - then your code should look like this:

class ThreadTwo(Thread):
def run(self):
tcpip.tcpip()

Or you don't subclass Thread and pass a target. But that target must be
a function. You don't pass a function, you call it!!

Look at this:

Thread(target=tcpip.tcpip)

Note the missing parentheses!

Apart from that, you should seriously consider applying a consistent
naming style to your code.

Diez- -

- -
Diez,
Thanks for your reply. I have tried the both you suggested. First
one works and second one does not. I am using "subclass thread" way to
implement my application. Thanks a lot.
ouyang

Jul 19 '07 #3

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

Similar topics

6
by: Sybren Stuvel | last post by:
Hi there, Is it possible to use an assignment in a while-loop? I'd like to do something like "loop while there is still something to be read, and if there is, put it in this variable". I've been...
2
by: mahurshi | last post by:
I am trying to read a file full of numbers followed by spaces (and then do some cool stuff with it) My input file looks like this 1 0 1 0 1 0 1 1 1 0 0 1 1 0
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
9
by: morpheus | last post by:
Hi Group, When I run this: # include <stdio.h> int main(){ int c=0; while ( (c=getchar()) != EOF && c != ' ' && c != '\t' ) printf("foo"); if (c == '8') putchar(c);
5
by: Achim Domma | last post by:
Hi, I have to convert a string to its "best possible" ascii representation. It's clear to me that this is not possible or sense full for all unicode characters. But for most European characters...
1
aprilmae36
by: aprilmae36 | last post by:
Nice day to all... Here I am again, having another problem with loops... lol... Can you please help me make a while loop program that the output will be like this: 1 121 12321 1234321...
1
by: DougFAI | last post by:
Hello there. Today, I was developing a college work software and then I saw thoe 2 types of while. My teacher asked me how does the "while(ifs)" (considering my ifstream was ifs) (that was the one...
2
by: recordlovelife | last post by:
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But...
3
by: toofuerte | last post by:
I have placed an input JOptionPane and get input and then qualify the input but then when I try to use the input to qualify when it is entered again it says the variable was not initialized. How do I...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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.