Global variables problem

Navkirat Singh navkirats at gmail.com
Wed Aug 4 00:48:52 EDT 2010


On 04-Aug-2010, at 9:46 AM, Daniel da Silva wrote:

> Please post approximate code that actually works and displays the problem. 
> 
> On Tue, Aug 3, 2010 at 9:00 PM, Navkirat Singh <navkirats at gmail.com> wrote:
> Hey guys,
> 
> 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:
> 
> 
> var = {}
> 
> class Something():
> 
>        def set_var(self):
>                global var
>                var = somevalue
> 
>        def get_var(self):
>                return var
> 
>        def newprocess(self):
>                self.set_var()
> 
>        def do_multiprocessing(self):
>                while true:
>                        self.get_var()
>                        new_process = process(target=newprocess)
>                        new_process.start()
> 
> 
> I am really confused here !
> 
> Any help would be awesome : )
> 
> Regards,
> Nav
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

This is a working code, streamlined, but it is where the problem is:

from multiprocessing import * 

dicts = 0
print('global ', dicts)

class WebServer():
	
	def set_sessionInfo(self):
		global dicts
		dicts = dicts + 1
	
	def get_sessionInfo(self):
		return dicts	

	def handle_connection(self):
		self.set_sessionInfo()
	
	def serve_forever(self):
		for x in range(10):
			p = Process(target=self.handle_connection)
			p.start()
			print(self.get_sessionInfo())
			
ws = WebServer()
ws.serve_forever()
print(dicts)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100804/cf632207/attachment-0001.html>


More information about the Python-list mailing list