简体   繁体   中英

Declare a global variable only in a context (with)

I don't know if it is possible, but i need to set a global object but only in a context (with)

for example, i have 2 part of code. the first is a worker object that read and write some files. but the worker don't actually know the real path of these files, so i have created a wrapper for redirect the open to the good filepath.

the probleme is that i create a wraper for every task ( with its own files) for the worker. i can't pass the wrapper for every function during the work. the best way to permit the working function to open through the wrapper is to set it global, but i am working with thread and i can't actually set it global (mutliple task with mutiple wrapper can be set up at the same time)

what i want is to do some thing like this:

with init_current_wrapper(task) as wrapper: #  do_some_magic_for_set_it_global(wrapper)

    do_some_jobs_for_this(task) # all process inside can access THIS wrapper

and inside the process under "do_some_jobs_for_this()" i could do

wrapper = get_current_wrapper()
wrapper.open("fakefilename")
#blablabla

without thread , a simple OSWrapper.get_current() could do it, but with thread,i can't know what is the "good" wrapper with the current context.

any idea ?

thanks

I think the clean solution to your problem is to pass the wrapper object around.

If that's impractical, you could make your global variable "thread-local" by using threading.local . However, I think the first approach (passing wrapper around) is far, far superior.

Instead of using a wrapper object to pass filenames to each thread, you might want to use a pool of worker threads that get the filename from a Queue.Queue object as in the following example:

import threading
import time
from Queue import Queue

NUM_FILES = 10
NUM_WORKERS = 3

queue = Queue()

def worker():
    print 'Thread started'
    while True:
        filename = queue.get()
        print 'Opening file:', filename
        # Do something with the file                                                                                                     
        time.sleep(1)
        print 'Closing file:', filename
        queue.task_done()


for i in range(NUM_WORKERS):
    thread = threading.Thread(target=worker)
    thread.daemon = True
    thread.start()

filenames = ['filename-{0}'.format(i)
             for i in range(NUM_FILES)]
for filename in filenames:
    queue.put(filename)

queue.join()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM