thread superposition bug in python

Asked

Viewed 43 times

1

Hello, I am studying the operation of threads and I made a program where there is a function that prints the current time of 'n' in 'n' seconds, and would like to create a parallel processing where I can play this function in a process to print the current time of 3 in 3 seconds and the other process of 5 in 5 seconds for example, this is the code I made:

import time
import datetime
import threading


def temporizador(worker, amplitude):
    # Não é necessário entender o funcionamento dessa função
    while True:
        unix_agora = (datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds()
        unix_agora_int = int(unix_agora)
        unix_agora_dec = unix_agora - unix_agora_int
        resto = int(unix_agora) % amplitude
        tempo_fim = int(unix_agora - resto - unix_agora_dec)
        print('worker', worker, '|', datetime.datetime.now())
        espera = tempo_fim + amplitude - (datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds()
        time.sleep(espera)



i = 0

threading.Thread(target=temporizador, args=('1', 3)).start()
threading.Thread(target=temporizador, args=('2', 5)).start()

The program works, but often the printable message of one thread supersedes the other by getting everything mixed up. This is what happens when I run the show:

worker 1 | 2019-06-04 21:08:50.108693

worker 2 | 2019-06-04 21:08:50.108693

worker 1 | 2019-06-04 21:08:51.002126

worker 1 | 2019-06-04 21:08:54.000400

worker 2 | 2019-06-04 21:08:55.001707

worker 1 | 2019-06-04 21:08:57.000754

worker 1 | 2019-06-04 21:09:00.001023

worker 2 | 2019-06-04 21:09:00.001023

worker 1 | 2019-06-04 21:09:03.000719

worker 2 | 2019-06-04 21:09:05.000572

worker 1 | 2019-06-04 21:09:06.001783

worker 1 | 2019-06-04 21:09:09.001796

worker 2 | 2019-06-04 21:09:10.001719

worker 1 | 2019-06-04 21:09:12.001740

worker 21 | |2019-06-04 21:09:15.001721 2019-06-04 21:09:15.001721

worker 1 | 2019-06-04 21:09:18.001723

worker 2 | 2019-06-04 21:09:20.001753

worker 1 | 2019-06-04 21:09:21.001057

.

How can I fix this bug? Thanks.

  • 2

    Not using thread. This is probably the same problem as this: https://answall.com/q/131126/101 Or: https://answall.com/q/18799/101

  • From what I understood then using threads is not a good idea, in this case, how could I make this process run parallel with more or less the same result that I got using thread?

  • 2

    Almost never, especially in Python, I don’t understand why people do this. Sure, there are cases that can be advantageous, but since this is one of the hardest things to use in computing it’s best to stay away unless you need a lot and be sure what you’re doing. https://answall.com/q/1946/101.

  • I understand, I’ll look for another alternative then, but I don’t know where to start, because I need functions that collect data in parallel, you have any suggestions of what I can do?

  • I can’t suggest because I don’t even know if you really need this.

  • 1

    Threads can be useful yes - in Python you have to know when and how - but even before that, you can learn the right way to deal with threads. Printing is one of the common promblemas, and I answered the correct approach (in Python, but it’s also the right way in any language) a few days ago, so I marked it as duplicate.

  • The fact that threads are not a good idea for real Python problems, unless you know what you’re doing, is because in the standard Python implementation there is a global lock on the virtual machine, which only lets one thread run Python code at a time, even on Cpus with multiple cores. If your code calls native functions that will make heavy lifiting, or as a "practical and inexpensive" way of dealing with input and output problems (for example, for web Scrapping, making multiple network requests in parallel), Python threads are a good yes.

  • 1

    @Maniero - you’re not "getting too close" to Python, are you? The language is popular yes, but there are reasons for this - I need to ask you to take a deep breath and think carefully about the objectivity of the comments and responses you have given about the language.

  • @Rafael: if your company is collecting data in parallel, threads can be a good solution - they are not the "most efficient" - but you would lose the main benefit of using Python - agility in development - if you were to worry about making an application using asyncio or a multiprocessing pool, if the problem is parallel web requests. If your problem is the print doesn’t line up, the answer I indicated should solve it. If there are more problems, just open the questions, with the specific problem.

  • 1

    @jsbueno no, I expose problems that she has. You’re not very passionate about her no? I need to ask you to take a deep breath and think carefully about the objectivity of the comments and answers, after all I say problems she has, you think you don’t. Either you think I’m bad-mouthing the language, or I said that something is bad for everything, and the impression is that it hurts you. Just now I spoke of threads almost all the time, just highlighted Python as a case that tends to be more problematic and needs more care, you hurt.

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.