473,804 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_com 1:
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.Threa d ):
def run ( self ):
print 'Thread', self.getName(), 'started.'
time.sleep ( 5 )
print 'Thread', self.getName(), 'ended.'

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

if __name__=="__ma in__":
spc = serial_port_com 1()
tcpip = TCP_IP()
thingOne = ThreadOne(targe t=spc.spc())
thingOne.start( )
thingTwo = ThreadTwo(targe t=tcpip.tcpip() )
thingTwo.start( )

Jul 17 '07 #1
2 1125
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_com 1:
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.Threa d ):
def run ( self ):
print 'Thread', self.getName(), 'started.'
time.sleep ( 5 )
print 'Thread', self.getName(), 'ended.'

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

if __name__=="__ma in__":
spc = serial_port_com 1()
tcpip = TCP_IP()
thingOne = ThreadOne(targe t=spc.spc())
thingOne.start( )
thingTwo = ThreadTwo(targe t=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(Threa d):
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=t cpip.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.w eb.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_com 1:
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.Threa d ):
def run ( self ):
print 'Thread', self.getName(), 'started.'
time.sleep ( 5 )
print 'Thread', self.getName(), 'ended.'
class ThreadTwo ( threading.Threa d ):
def run ( self ):
print 'Thread', self.getName(), 'started.'
thingOne.join()
print 'Thread', self.getName(), 'ended.'
if __name__=="__ma in__":
spc = serial_port_com 1()
tcpip = TCP_IP()
thingOne = ThreadOne(targe t=spc.spc())
thingOne.start( )
thingTwo = ThreadTwo(targe t=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(Threa d):
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=t cpip.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
12219
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 a C programmer since I was 14, so a construct like: while info = mydbcursor.fetchone(): print "Information: "+str(info)
2
4234
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
71984
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
1649
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
2863
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 it should be possible. For example: "Müller" should become "Muller" and "é" should become "e".
1
1271
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 123454321 :-)
1
1835
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 I was using) should work, I didn't know what to answer. Both of em works to check the enf of the file in, right? Whats the advantage in using while(ifs)? How does the "ifs" works. Thanks in the advance. :)
2
3234
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 on the homepage of a site, i wanted to truncate the content to like the first 75 characters and then put "..." (a perfect use of the smarty "truncate" modifier) and then give the visitor a link to read the whole article. But since "the_content" is a...
3
1562
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 get the input info out of the While into the rest of the program? import javax.swing.*; public class Password { public static void main (String args) { String len= "f";//Length correct String...
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10350
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10096
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.