0

How should I go about creating a counter that will be decremented when a multiprocessing.process is decrementing the counter variable from the main process? With the limited C knowledge and programming knowledge I have, I believe a pointer would be very useful here. Is there some equivalent?

For example:

counter = 400

def decrement(counter):
    global counter
    while counter > 0:
       counter = counter - 1
    return counter

if __name__ == '__main__':
    p = multiprocessing.process(target=decrement(counter))
    p.start()
    while counter != 0:
        print(counter)
    ...

My goal here is for it to be shown how many "Clicks" are left from a set value for my autoclicker. In the code example, I want it to show it decreasing from 400 to 0. Please let me know if this is possible in the current implementation chosen. I'd put a image of my autoclicker below but unfortunately I don't have enough "reputation" to post it.

Edit: I've tried using Value from the multiprocessing library as well as Queue to send over the information, but the problem is I need a thread to update the variable while the process is running. It would be mighty convenient to have the process have access to the variable so it can update it directly.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Does this answer your question? [multiprocessing global variable updates not returned to parent](https://stackoverflow.com/questions/11055303/multiprocessing-global-variable-updates-not-returned-to-parent) – shoaib30 Jul 29 '21 at 15:56
  • probably need to use a multiprocessing Manager – acushner Jul 29 '21 at 16:05
  • Does this answer your question? [Using a global variable with a thread](https://stackoverflow.com/questions/19790570/using-a-global-variable-with-a-thread) (note you will want to incorporate `lock()` with the accepted answer) – JonSG Jul 29 '21 at 16:24
  • Do you mean `multiprocessing` not `multiprocessor`? If so, you may be able to use [`multiprocessing.shared_memory`](https://docs.python.org/3/library/multiprocessing.shared_memory.html#module-multiprocessing.shared_memory). – martineau Jul 29 '21 at 16:55
  • @martineau Yes! Sorrry, I meant Multiprocessing... I'll look more into it thanks – Joey Rolfe Jul 29 '21 at 17:51
  • OK, then I think my suggestion would work. – martineau Jul 29 '21 at 17:59
  • @JonSG No, i'm using processes not threads sorry Jon – Joey Rolfe Jul 29 '21 at 18:01

0 Answers0