Your problem lies somewhere in the use of the Process class, not with global variables.<br><br>If you replace your "p = ..." and "p.start()" lines with a direct call to self.handle_connection(), your code works as expected. I don't know much about the multiprocessing module, so I can't really comment on what you're doing wrong, but I hope this points you in the right direction.<br>
<br>Sorry I couldn't be of more help,<br><br>Daniel<br><br><br><div class="gmail_quote">On Tue, Aug 3, 2010 at 9:48 PM, Navkirat Singh <span dir="ltr"><<a href="mailto:navkirats@gmail.com">navkirats@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word;"><div><div></div><div class="h5"><br><div><div>On 04-Aug-2010, at 9:46 AM, Daniel da Silva wrote:</div>
<br><blockquote type="cite">Please post approximate code that actually works and displays the problem. <br><br><div class="gmail_quote">On Tue, Aug 3, 2010 at 9:00 PM, Navkirat Singh <span dir="ltr"><<a href="mailto:navkirats@gmail.com" target="_blank">navkirats@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">Hey guys,<br>
<br>
I am using a multiprocessing program, where the new process is supposed to change a variable in the main class that it branches out from. This is somehow not working, following is an approximate code. Would really appreciate any insight into this matter:<br>
<br>
<br>
var = {}<br>
<br>
class Something():<br>
<br>
def set_var(self):<br>
global var<br>
var = somevalue<br>
<br>
def get_var(self):<br>
return var<br>
<br>
def newprocess(self):<br>
self.set_var()<br>
<br>
def do_multiprocessing(self):<br>
while true:<br>
self.get_var()<br>
new_process = process(target=newprocess)<br>
new_process.start()<br>
<br>
<br>
I am really confused here !<br>
<br>
Any help would be awesome : )<br>
<br>
Regards,<br>
Nav<br>
<font color="#888888"><br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br>
</blockquote><br></div></div></div><div>This is a working code, streamlined, but it is where the problem is:</div><div><br></div><div><div>from multiprocessing import * </div><div><br></div><div>dicts = 0</div><div>print('global ', dicts)</div>
<div><br></div><div>class WebServer():</div><div><span style="white-space: pre-wrap;"> </span></div><div><span style="white-space: pre-wrap;"> </span>def set_sessionInfo(self):</div><div><span style="white-space: pre-wrap;"> </span>global dicts</div>
<div><span style="white-space: pre-wrap;"> </span>dicts = dicts + 1</div><div><span style="white-space: pre-wrap;"> </span></div><div><span style="white-space: pre-wrap;"> </span>def get_sessionInfo(self):</div><div><span style="white-space: pre-wrap;"> </span>return dicts<span style="white-space: pre-wrap;"> </span></div>
<div><br></div><div><span style="white-space: pre-wrap;"> </span>def handle_connection(self):</div><div><span style="white-space: pre-wrap;"> </span>self.set_sessionInfo()</div><div><span style="white-space: pre-wrap;"> </span></div>
<div><span style="white-space: pre-wrap;"> </span>def serve_forever(self):</div><div><span style="white-space: pre-wrap;"> </span>for x in range(10):</div><div><span style="white-space: pre-wrap;"> </span>p = Process(target=self.handle_connection)</div>
<div><span style="white-space: pre-wrap;"> </span>p.start()</div><div><span style="white-space: pre-wrap;"> </span>print(self.get_sessionInfo())</div><div><span style="white-space: pre-wrap;"> </span></div><div>ws = WebServer()</div>
<div>ws.serve_forever()</div><div>print(dicts)</div><div><br></div></div><br></div></blockquote></div><br>