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

Why my thread can't access the global data?

ddh
Hi,
I got a problem. I use a 'select' in a loop in the main thread, and
when select return, a new thread will be created to handle the network
event. And if the client send some special string, the thread will
change a global flag to false, so the loop in the main thread will
break. But it never work.

Below is the code:
--------------------- s.py (the server) --------------------------
import socket
import select
import thread
import sys

go_on = True

def read_send(s):
s.setblocking(1)
str = s.recv(1024)
print 'recv:', str
s.send(str)
s.close()
if (str == 'quit'):
go_on = False
print 'User quit...with go_on =', go_on
return
s = socket.socket(socket.AF_INET)
s.bind(('', 9999))
s.listen(5)
s.setblocking(0)

while go_on:
r_set = [s.fileno(),]
r, w, e = select.select(r_set,[],[],0.5)
print 'select returned with go_on =', go_on
for rs in r:
if rs == s.fileno():
ns, addr = s.accept()
print 'socket on', addr, 'has been accepted'
thread.start_new_thread(read_send, (ns,))
s.close()
--------------------- c.py (the client) --------------------------
import socket
import sys

if len(sys.argv) != 3:
print 'usage: python c.py ip port'
sys.exit(-1)

ip = sys.argv[1]
port = int(sys.argv[2])
s = socket.socket(socket.AF_INET)
s.settimeout(5)
s.connect((ip, port))
str = raw_input('please input:')
s.send(str)
str = s.recv(1024)
print 'received:', str
s.close()

--------------------------------------------------------------------------
run s.py first, and then run c.py as:
python c.py 127.0.0.1 9999
and then input 'quit', but the server never end :(

Thank you for your help!

Dec 21 '05 #1
4 1425
your main loop already on accept when your thread change the go_on imho
try to input another string after 'quit'

and actually there is no need to use thread

Dec 21 '05 #2
ddh
Thank you, but I think it may be not this reason.

You see, when accept returns, the go_on will be checked in 'while
go_on:', so if it is set to be false, the loop will end. I have set a
0.5 second time out on the select() function. So the 'go_on' will be
checked at a frequency every 0.5 second at least.


Pelmen wrote:
your main loop already on accept when your thread change the go_on imho
try to input another string after 'quit'

and actually there is no need to use thread


Dec 21 '05 #3
Op 2005-12-21, ddh schreef <do******@gmail.com>:
Hi,
I got a problem. I use a 'select' in a loop in the main thread, and
when select return, a new thread will be created to handle the network
event. And if the client send some special string, the thread will
change a global flag to false, so the loop in the main thread will
break. But it never work.

Below is the code:
--------------------- s.py (the server) --------------------------
import socket
import select
import thread
import sys

go_on = True

def read_send(s):
s.setblocking(1)
str = s.recv(1024)
print 'recv:', str
s.send(str)
s.close()
if (str == 'quit'):
go_on = False
print 'User quit...with go_on =', go_on
return

[ ... ]


It has nothing to do with threads. If you assign to a name in
a function, that name will be treated as a local variable. So
the go_on = False in read_send will not affect the global go_on.

If you want to rebind global names in a function you have to use
the global statement. So your function would start:
def read_send(s):
global go_on
s.setblocking(1)
...

--
Antoon Pardon
Dec 21 '05 #4
ddh
Thank you very much.
I am a newbie for python :-)
Antoon Pardon wrote:

It has nothing to do with threads. If you assign to a name in
a function, that name will be treated as a local variable. So
the go_on = False in read_send will not affect the global go_on.

If you want to rebind global names in a function you have to use
the global statement. So your function would start:
def read_send(s):
global go_on
s.setblocking(1)
...

--
Antoon Pardon


Dec 21 '05 #5

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

Similar topics

5
by: nicolas_riesch | last post by:
Does someone know if the module pytz (http://sourceforge.net/projects/pytz/) is thread-safe ? I have not seen it explicitely stated, and just wanted to be sure, as I want to use it. That's...
5
by: Alvin Bruney | last post by:
I dispensed some advice and its bugging me that it may not be 100% accurate. Worker threads should not touch main thread objects. Everybody knows that but if you pass a reference to a form object...
1
by: William Sullivan | last post by:
I'm trying to nail down some issues with the cache in my application. Currently, I have an object that stands between my business logic and database logic called CacheLogic (cute, no?). ...
7
by: Mythran | last post by:
Been 11 days since I posted this and 0 replies (although, in OE, it looks like there was 1 but it's just another post with the same subject as before): Part #1: I have a Thread, MainThread, and...
2
by: Mike | last post by:
Hello, Ok I have 2 classes in my project, one is the main form and one is a connection class, at a certain event on my main form a new instance is made of the connection class, and a reference...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
6
by: Lars Uffmann | last post by:
In an event routine, I want to end a certain thread. I am setting a flag that is checked by the thread and causes it to end, when it is set. Then the thread sets a "response" flag, just before...
19
by: Hapa | last post by:
Does only reading (never writing) of a variable need thread synchronisation? Thanks for help? PS. Anybody knows a Visual C++ news group?
0
by: davy zhang | last post by:
I wrote this server to handle incoming messages in a process using multiprocessing named "handler", and sending message in a Thread named "sender", 'cause I think the async_chat object can not...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.