473,231 Members | 1,477 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,231 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 1104
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.