Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threading and Queue
#1
I'm working on a backup script for home use and at the same time learn threading. I was researching about threads and sharing data across threads. All my research lead to queues, and I found examples from the Python documentation and Google. Here is the best I can come up with.

Purpose:
Read data in from a text file in a separate thread and return it to the main thread for displaying.

Problem:
When printing, it prints the text with brackets. For example, KeePass is the text stored in the file, it prints it like this:

['KeePass']

How can I remove the brackets? I need to work with the values in other methods

import threading
import queue


def construct_list(read_file, backup):
    with open(read_file) as read_obj:
        backup.put(read_obj.readlines())
    backup.task_done()


backup_list = queue.Queue()
read_thread = threading.Thread(target=construct_list, args=("list.txt", backup_list,))


read_thread.start()
read_thread.join()

while backup_list.empty() is False:
    print(backup_list.get())
Reply
#2
If it is a list, it will print enclosed in brackets, to indicate to the viewer that is is indeed a list.
If you want to print the elements in a list, do:
mylist = ['apples', 'pears', 'oranges']
for item in mylist:
    print(item)
# to print individual items (remembering that the index is zero based)
[python]print(mylist[1])
will print pears.

[/python]
Reply
#3
(Oct-16-2017, 04:31 AM)Larz60+ Wrote: If it is a list, it will print enclosed in brackets, to indicate to the viewer that is is indeed a list.
If you want to print the elements in a list, do:
mylist = ['apples', 'pears', 'oranges']
for item in mylist:
    print(item)
# to print individual items (remembering that the index is zero based)
[python]print(mylist[1])
will print pears.

[/python]

Problem is, it's a Queue. Can't use a for loop
Reply
#4
You can convert to list:
import queue

q = queue.Queue()

for x in range(4):
    q.put(x)

z = list(q.queue)
print(z)
Reply
#5
(Oct-16-2017, 03:52 PM)Larz60+ Wrote: You can convert to list:
import queue

q = queue.Queue()

for x in range(4):
    q.put(x)

z = list(q.queue)
print(z)

Thanks for the feedback. How did you learn multi threading? I posted this same code on SO to ask if I was using Queue properly, and the result was less than pleasing. What resources did you use to learn multi threading? I prefer textbooks, because I attempted to Google, but from the pages I found and the code I produced, it was bad advice.
Reply
#6
Actually, I don't use it much, (although I have in several applications),
and I can't really tell you where I learned it. Whenever I need to learn
something new, I'm all over the web looking for bits and pieces until I
have a grasp on the subject.

A good bet it that I learned it right here in the forum!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,809 May-03-2023, 08:22 AM
Last Post: billykid999
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,113 Oct-01-2021, 08:32 PM
Last Post: muzikman
  task queue Valon1981 8 3,592 Jul-07-2020, 07:41 AM
Last Post: freeman
  Queue in Pygame constantin01 1 3,676 Jan-07-2020, 04:02 PM
Last Post: metulburr
  Queue maxsize mr_byte31 2 4,540 Sep-03-2019, 07:02 PM
Last Post: mr_byte31
  Queue.Queue() would not reduce capacity after get() yuan8421 9 11,057 Jan-02-2018, 09:38 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020