0

whats the best way to share string between two Python threads?

i am running a method in a second thread and i want to share some data between those threads.

    # arr = Array(c_char_p, 3)
    val = multiprocessing.Value(c_char)
    p = multiprocessing.Process(target=process_data, args=(val,))

i want to share string between those processes. And i found articels which indicate, that i should use c_char_p for this. However i keep getting this error: "ValueError: invalid string pointer 0x02A82214"

I googled it and i found that c_char_p is a pointer which leads to problems if it is used between different threads. And instead i should use c_char (without the p) With c_char it works as long as i only have one single character. However i want to share a complete string (and i don't know exactly the length of this string)

Whats the cleanest way to do this?

If you want to know more detailed what i want to do, you can read this. Otherwise you can skip:

i want to process some data in the background and a dialog (which should run in the main thread) with a progress bar should show the progress. The progress doesn't only show a percentage but also a String-message which indicates, what exactly is done at the moment. So i have to share this string message between the two threads.

Can someone tell me what is the best way to do this?

Thanks a lot!

mak1
  • 33
  • 4
  • Wouldn't it be easier (and safer) for your threads to communicate this message instead of sharing? The main thread can send a message to the progress bar thread with the percent and the message to display. You can easily achieve this with a queue – rdas Apr 06 '19 at 19:54
  • I think python tqdm package should work here. Check this thread https://stackoverflow.com/a/40133278/6069517 – raj Apr 06 '19 at 19:56
  • thanks for the hint how would you recommend to communicate messages? Would a Queue be a good solution? – mak1 Apr 06 '19 at 20:14
  • Just a minor suggestion, you might want to be more accurate with your terms regarding thread and process – benjamin Apr 06 '19 at 20:36
  • oh right... i guess what i am talking about is always a process. but i hope it is still clear what i wanted to know – mak1 Apr 06 '19 at 20:55

0 Answers0